00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#include <koTemplateCreateDia.h>
00024
00025
#include <qlayout.h>
00026
#include <qlabel.h>
00027
#include <qgroupbox.h>
00028
#include <qradiobutton.h>
00029
#include <qpushbutton.h>
00030
#include <qheader.h>
00031
00032
#include <ktempfile.h>
00033
#include <klineedit.h>
00034
#include <klistview.h>
00035
#include <klocale.h>
00036
#include <koTemplates.h>
00037
#include <kfiledialog.h>
00038
#include <klineeditdlg.h>
00039
#include <kmessagebox.h>
00040
#include <kimageio.h>
00041
#include <kstandarddirs.h>
00042
#include <kdebug.h>
00043
#include <kio/netaccess.h>
00044
00045
#include <stdlib.h>
00046
#include <kinstance.h>
00047
00048
00049
class KoTemplateCreateDiaPrivate {
00050
public:
00051 KoTemplateCreateDiaPrivate()
00052 : m_tempFile(
QString::null, ".png" )
00053 {
00054 m_tree=0L;
00055 m_name=0L;
00056 m_default=0L;
00057 m_custom=0L;
00058 m_select=0L;
00059 m_preview=0L;
00060 m_groups=0L;
00061 m_add=0L;
00062 m_remove=0L;
00063 m_tempFile.setAutoDelete(
true );
00064 }
00065 ~KoTemplateCreateDiaPrivate() {
00066
delete m_tree;
00067 }
00068
00069 KoTemplateTree *m_tree;
00070 KLineEdit *m_name;
00071
QRadioButton *m_default, *m_custom;
00072
QPushButton *m_select;
00073
QLabel *m_preview;
00074
QString m_customFile;
00075
QPixmap m_customPixmap;
00076 KListView *m_groups;
00077
QPushButton *m_add, *m_remove;
00078
bool m_changed;
00079 KTempFile m_tempFile;
00080 };
00081
00082
00083
00084
00085
00086
00087
00088
00089 KoTemplateCreateDia::KoTemplateCreateDia(
const QCString &templateType, KInstance *instance,
00090
const QString &file,
const QPixmap &pix,
QWidget *parent ) :
00091 KDialogBase( parent, "template create dia", true, i18n( "Create Template" ),
00092 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok ), m_file(file), m_pixmap(pix) {
00093
00094 d=
new KoTemplateCreateDiaPrivate();
00095
00096
QFrame *mainwidget=makeMainWidget();
00097
QHBoxLayout *mbox=
new QHBoxLayout(mainwidget, KDialogBase::marginHint(),
00098 KDialogBase::spacingHint());
00099
QVBoxLayout *leftbox=
new QVBoxLayout(mbox);
00100
00101
QLabel *label=
new QLabel(i18n(
"Name:"), mainwidget);
00102 leftbox->addSpacing(label->fontMetrics().height()/2);
00103 QHBoxLayout *namefield=
new QHBoxLayout(leftbox);
00104 namefield->addWidget(label);
00105 d->m_name=
new KLineEdit(mainwidget);
00106 d->m_name->setFocus();
00107 connect(d->m_name, SIGNAL(textChanged(
const QString &)),
00108
this, SLOT(slotNameChanged(
const QString &)));
00109 namefield->addWidget(d->m_name);
00110
00111 label=
new QLabel(i18n(
"Group:"), mainwidget);
00112 leftbox->addWidget(label);
00113 d->m_groups=
new KListView(mainwidget);
00114 leftbox->addWidget(d->m_groups);
00115 d->m_groups->addColumn(
"");
00116 d->m_groups->header()->hide();
00117 d->m_groups->setRootIsDecorated(
true);
00118 d->m_groups->setSorting(0);
00119
00120 d->m_tree=
new KoTemplateTree(templateType, instance,
true);
00121 fillGroupTree();
00122 d->m_groups->sort();
00123
00124 QHBoxLayout *bbox=
new QHBoxLayout(leftbox);
00125 d->m_add=
new QPushButton(i18n(
"&Add Group..."), mainwidget);
00126 connect(d->m_add, SIGNAL(clicked()),
this, SLOT(slotAddGroup()));
00127 bbox->addWidget(d->m_add);
00128 d->m_remove=
new QPushButton(i18n(
"&Remove"), mainwidget);
00129 connect(d->m_remove, SIGNAL(clicked()),
this, SLOT(slotRemove()));
00130 bbox->addWidget(d->m_remove);
00131
00132 QVBoxLayout *rightbox=
new QVBoxLayout(mbox);
00133
QGroupBox *pixbox=
new QGroupBox(i18n(
"Picture"), mainwidget);
00134 rightbox->addWidget(pixbox);
00135 QVBoxLayout *pixlayout=
new QVBoxLayout(pixbox, KDialogBase::marginHint(),
00136 KDialogBase::spacingHint());
00137 pixlayout->addSpacing(pixbox->fontMetrics().height()/2);
00138 pixlayout->addStretch(1);
00139 d->m_default=
new QRadioButton(i18n(
"&Default"), pixbox);
00140 d->m_default->setChecked(
true);
00141 connect(d->m_default, SIGNAL(clicked()),
this, SLOT(slotDefault()));
00142 pixlayout->addWidget(d->m_default);
00143 QHBoxLayout *custombox=
new QHBoxLayout(pixlayout);
00144 d->m_custom=
new QRadioButton(i18n(
"Custom"), pixbox);
00145 d->m_custom->setChecked(
false);
00146 connect(d->m_custom, SIGNAL(clicked()),
this, SLOT(slotCustom()));
00147 custombox->addWidget(d->m_custom);
00148 d->m_select=
new QPushButton(i18n(
"&Select..."), pixbox);
00149 connect(d->m_select, SIGNAL(clicked()),
this, SLOT(slotSelect()));
00150 custombox->addWidget(d->m_select, 1);
00151 custombox->addStretch(1);
00152 pixlayout->addStretch(1);
00153 label=
new QLabel(i18n(
"Preview:"), pixbox);
00154 pixlayout->addWidget(label);
00155 QHBoxLayout *previewbox=
new QHBoxLayout(pixlayout);
00156 previewbox->addStretch(10);
00157 d->m_preview=
new QLabel(pixbox);
00158 previewbox->addWidget(d->m_preview);
00159 previewbox->addStretch(10);
00160 pixlayout->addStretch(8);
00161
00162 enableButtonOK(
false);
00163 d->m_changed=
false;
00164 updatePixmap();
00165
00166 connect(d->m_groups,SIGNAL( selectionChanged()),
this,SLOT(slotSelectionChanged()));
00167
00168 d->m_remove->setEnabled(d->m_groups->currentItem());
00169 }
00170
00171 KoTemplateCreateDia::~KoTemplateCreateDia() {
00172
delete d;
00173 }
00174
00175
void KoTemplateCreateDia::slotSelectionChanged()
00176 {
00177
const QListViewItem* item = d->m_groups->currentItem();
00178 d->m_remove->setEnabled( item );
00179
if ( ! item )
00180
return;
00181
00182
if ( item->depth() > 0 )
00183 d->m_name->setText( item->text( 0 ) );
00184 }
00185
00186
void KoTemplateCreateDia::createTemplate(
const QCString &templateType, KInstance *instance,
00187
const QString &file,
const QPixmap &pix,
QWidget *parent ) {
00188
00189 KoTemplateCreateDia *dia =
new KoTemplateCreateDia( templateType, instance, file, pix, parent );
00190 dia->exec();
00191
delete dia;
00192 }
00193
00194
void KoTemplateCreateDia::slotOk() {
00195
00196
00197
QListViewItem *item=d->m_groups->currentItem();
00198
if(!item)
00199 item=d->m_groups->firstChild();
00200
if(!item) {
00201 d->m_tree->writeTemplateTree();
00202 KDialogBase::slotCancel();
00203
return;
00204 }
00205
00206
if(item->depth()!=0)
00207 item=item->parent();
00208
if(!item) {
00209 d->m_tree->writeTemplateTree();
00210 KDialogBase::slotCancel();
00211
return;
00212 }
00213
00214 KoTemplateGroup *group=d->m_tree->find(item->text(0));
00215
if(!group) {
00216 d->m_tree->writeTemplateTree();
00217 KDialogBase::slotCancel();
00218
return;
00219 }
00220
00221
if(d->m_name->text().isEmpty()) {
00222 d->m_tree->writeTemplateTree();
00223 KDialogBase::slotCancel();
00224
return;
00225 }
00226
00227
00228
QString dir=d->m_tree->instance()->dirs()->saveLocation(d->m_tree->templateType());
00229 dir+=KoTemplates::stripWhiteSpace(group->name());
00230
QString templateDir=dir+
"/.source/";
00231
QString iconDir=dir+
"/.icon/";
00232
00233
QString file=KoTemplates::stripWhiteSpace(d->m_name->text());
00234
QString tmpIcon=
".icon/"+file;
00235 tmpIcon+=
".png";
00236
QString icon=iconDir+file;
00237 icon+=
".png";
00238
00239
00240
unsigned int k=0;
00241
unsigned int foo=m_file.length();
00242
while(m_file[foo-k]!=
QChar(
'.') && k<=foo) {
00243 ++k;
00244 }
00245
if(k<foo)
00246 file+=m_file.right(k);
00247
00248 KoTemplate *t=
new KoTemplate(d->m_name->text(), QString::null,
".source/"+file, tmpIcon,
false,
true);
00249
if(!group->add(t)) {
00250 KoTemplate *existingTemplate=group->find(t->name());
00251
00252
if(existingTemplate && existingTemplate->isHidden())
00253 group->add(t,
true);
00254
00255
else if(existingTemplate && !existingTemplate->isHidden()) {
00256
if(KMessageBox::warningYesNo(
this, i18n(
"Do you really want to overwrite"
00257
" the existing '%1' template?").
00258 arg(existingTemplate->name()))==KMessageBox::Yes)
00259 group->add(t,
true);
00260
else
00261 {
00262
delete t;
00263
return;
00264 }
00265 }
00266 }
00267
00268
if(!KStandardDirs::makeDir(templateDir) || !KStandardDirs::makeDir(iconDir)) {
00269 d->m_tree->writeTemplateTree();
00270 KDialogBase::slotCancel();
00271
return;
00272 }
00273
00274
00275 KURL dest;
00276 dest.setPath(templateDir+file);
00277 KIO::NetAccess::del( dest );
00278 KIO::NetAccess::copy(m_file, dest);
00279
00280
00281
bool ready=
false;
00282
QStringList tmp=group->dirs();
00283
for(QStringList::ConstIterator it=tmp.begin(); it!=tmp.end() && !ready; ++it) {
00284
if((*it).contains(dir)==0) {
00285
QString f=(*it)+
".directory";
00286
QFileInfo info(f);
00287
if(info.exists()) {
00288 dest.setPath( dir+
"/.directory" );
00289 KIO::NetAccess::del( dest );
00290 KIO::NetAccess::copy( f, dest );
00291 ready=
true;
00292 }
00293 }
00294 }
00295
00296
if(d->m_default->isChecked() && !m_pixmap.isNull())
00297 m_pixmap.save(icon,
"PNG");
00298
else if(!d->m_customPixmap.isNull())
00299 d->m_customPixmap.save(icon,
"PNG");
00300
else
00301 kdWarning(30004) <<
"Could not save the preview picture!" << endl;
00302
00303 d->m_tree->writeTemplateTree();
00304 KDialogBase::slotOk();
00305 }
00306
00307
void KoTemplateCreateDia::slotDefault() {
00308
00309 d->m_default->setChecked(
true);
00310 d->m_custom->setChecked(
false);
00311 updatePixmap();
00312 }
00313
00314
void KoTemplateCreateDia::slotCustom() {
00315
00316 d->m_default->setChecked(
false);
00317 d->m_custom->setChecked(
true);
00318
if(d->m_customFile.isEmpty())
00319 slotSelect();
00320
else
00321 updatePixmap();
00322 }
00323
00324
void KoTemplateCreateDia::slotSelect() {
00325
00326 d->m_default->setChecked(
false);
00327 d->m_custom->setChecked(
true);
00328
00329 KFileDialog fd(QString::null, KImageIO::pattern(KImageIO::Reading), 0, 0,
true);
00330 fd.setCaption(i18n(
"Select Picture"));
00331 KURL url;
00332
if (fd.exec()==QDialog::Accepted)
00333 url=fd.selectedURL();
00334
00335
if(url.isEmpty()) {
00336
if(d->m_customFile.isEmpty()) {
00337 d->m_default->setChecked(
true);
00338 d->m_custom->setChecked(
false);
00339 }
00340
return;
00341 }
00342
00343
if ( url.isLocalFile() )
00344 {
00345 d->m_customFile = url.path();
00346 }
00347
else
00348 {
00349
QString target ( d->m_tempFile.name() );
00350
if ( KIO::NetAccess::download( url, target ) )
00351 {
00352 d->m_customFile = target;
00353 }
00354
else
00355 {
00356 KMessageBox::sorry( 0L, i18n(
"Could not open\n%1" ).arg( url.prettyURL() ) );
00357
return;
00358 }
00359 }
00360
00361 d->m_customPixmap=
QPixmap();
00362 updatePixmap();
00363 }
00364
00365
void KoTemplateCreateDia::slotNameChanged(
const QString &name) {
00366
00367
if((name.isEmpty() || !d->m_groups->firstChild()) && !d->m_changed)
00368 enableButtonOK(
false);
00369
else
00370 enableButtonOK(
true);
00371 }
00372
00373
void KoTemplateCreateDia::slotAddGroup() {
00374
bool ok=
false;
00375
QString name=KLineEditDlg::getText( i18n(
"Add Group"),i18n(
"Enter group name:"),QString::null, &ok,
this );
00376
if(!ok)
00377
return;
00378 KoTemplateGroup *group=d->m_tree->find(name);
00379
if(group && !group->isHidden())
00380
return;
00381
00382
QString dir=d->m_tree->instance()->dirs()->saveLocation(d->m_tree->templateType());
00383 dir+=name;
00384 KoTemplateGroup *newGroup=
new KoTemplateGroup(name, dir,
true);
00385 d->m_tree->add(newGroup);
00386
QListViewItem *item=
new QListViewItem(d->m_groups, name);
00387 d->m_groups->setCurrentItem(item);
00388 d->m_groups->sort();
00389 d->m_name->setFocus();
00390 enableButtonOK(
true);
00391 d->m_changed=
true;
00392 }
00393
00394
void KoTemplateCreateDia::slotRemove() {
00395
00396
QListViewItem *item=d->m_groups->currentItem();
00397
if(!item)
00398
return;
00399
00400
QString what;
00401
QString removed;
00402
if (item->depth()==0) {
00403 what = i18n(
"Do you really want to remove that group?");
00404 removed = i18n(
"Remove Group");
00405 }
else {
00406 what = i18n(
"Do you really want to remove that template?");
00407 removed = i18n(
"Remove Template");
00408 }
00409
00410
if(KMessageBox::warningYesNo(
this, what,
00411 removed)==KMessageBox::No) {
00412 d->m_name->setFocus();
00413
return;
00414 }
00415
00416
if(item->depth()==0) {
00417 KoTemplateGroup *group=d->m_tree->find(item->text(0));
00418
if(group)
00419 group->setHidden(
true);
00420 }
00421
else {
00422
bool done=
false;
00423
for(KoTemplateGroup *g=d->m_tree->first(); g!=0L && !done; g=d->m_tree->next()) {
00424 KoTemplate *t=g->find(item->text(0));
00425
if(t) {
00426 t->setHidden(
true);
00427 done=
true;
00428 }
00429 }
00430 }
00431
delete item;
00432 item=0L;
00433 enableButtonOK(
true);
00434 d->m_name->setFocus();
00435 d->m_changed=
true;
00436 }
00437
00438
void KoTemplateCreateDia::updatePixmap() {
00439
00440
if(d->m_default->isChecked() && !m_pixmap.isNull())
00441 d->m_preview->setPixmap(m_pixmap);
00442
else if(d->m_custom->isChecked() && !d->m_customFile.isEmpty()) {
00443
if(d->m_customPixmap.isNull()) {
00444
00445 KoTemplate t(
"foo",
"bar", QString::null, d->m_customFile);
00446 d->m_customPixmap=t.loadPicture();
00447 }
00448
if(!d->m_customPixmap.isNull())
00449 d->m_preview->setPixmap(d->m_customPixmap);
00450
else
00451 d->m_preview->setText(i18n(
"Could not load picture."));
00452 }
00453
else
00454 d->m_preview->setText(i18n(
"No picture available."));
00455 }
00456
00457
void KoTemplateCreateDia::fillGroupTree() {
00458
00459
for(KoTemplateGroup *group=d->m_tree->first(); group!=0L; group=d->m_tree->next()) {
00460
if(group->isHidden())
00461
continue;
00462
QListViewItem *groupItem=
new QListViewItem(d->m_groups, group->name());
00463
for(KoTemplate *t=group->first(); t!=0L; t=group->next()) {
00464
if(t->isHidden())
00465
continue;
00466 (
void)
new QListViewItem(groupItem, t->name());
00467 }
00468 }
00469 }
00470
00471
#include <koTemplateCreateDia.moc>