28 #include <KFileDialog>
29 #include <KMessageBox>
32 #include <KStandardDirs>
34 #include <KIO/NetAccess>
37 #include <QHBoxLayout>
38 #include <QHeaderView>
40 #include <QPushButton>
41 #include <QTreeWidget>
45 class PageItem :
public QTreeWidgetItem
48 PageItem( QTreeWidget *parent,
const QString &path )
49 : QTreeWidgetItem( parent ),
50 mPath( path ), mIsActive( false )
52 setFlags( flags() | Qt::ItemIsUserCheckable );
53 setCheckState( 0, Qt::Unchecked );
54 mName = path.mid( path.lastIndexOf( QLatin1Char(
'/') ) + 1 );
57 if (!f.open( QFile::ReadOnly ) )
60 QWidget *wdg = builder.load( &f, 0 );
63 setText( 0, wdg->windowTitle() );
65 QPixmap pm = QPixmap::grabWidget( wdg );
66 QImage img = pm.toImage().scaled( 300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation );
67 mPreview = QPixmap::fromImage(img);
69 QMap<QString, QString> allowedTypes;
70 allowedTypes.insert( QLatin1String(
"QLineEdit"), i18n(
"Text" ) );
71 allowedTypes.insert( QLatin1String(
"QTextEdit"), i18n(
"Text" ) );
72 allowedTypes.insert( QLatin1String(
"QSpinBox"), i18n(
"Numeric Value" ) );
73 allowedTypes.insert( QLatin1String(
"QCheckBox"), i18n(
"Boolean" ) );
74 allowedTypes.insert( QLatin1String(
"QComboBox"), i18n(
"Selection" ) );
75 allowedTypes.insert( QLatin1String(
"QDateTimeEdit"), i18n(
"Date & Time" ) );
76 allowedTypes.insert( QLatin1String(
"KLineEdit"), i18n(
"Text" ) );
77 allowedTypes.insert( QLatin1String(
"KTextEdit"), i18n(
"Text" ) );
78 allowedTypes.insert( QLatin1String(
"KDateTimeWidget"), i18n(
"Date & Time" ) );
79 allowedTypes.insert( QLatin1String(
"KDatePicker"), i18n(
"Date" ) );
81 QList<QWidget*> list = wdg->findChildren<
QWidget*>();
83 Q_FOREACH ( it, list ) {
84 if ( allowedTypes.contains( QLatin1String(it->metaObject()->className()) ) ) {
85 QString name = it->objectName();
86 if ( name.startsWith( QLatin1String(
"X_" ) ) ) {
87 new QTreeWidgetItem(
this, QStringList()
89 << allowedTypes[ QLatin1String(it->metaObject()->className()) ]
90 << QLatin1String(it->metaObject()->className())
114 void setIsActive(
bool isActive )
116 mIsActive = isActive;
119 bool isActive()
const
126 return checkState( 0 ) == Qt::Checked;
137 const QVariantList &args )
138 :
KCModule( instance, parent, args ),
147 ki18n(
"Qt Designer Fields Dialog" ),
148 0, KLocalizedString(), KAboutData::License_LGPL,
149 ki18n(
"(c), 2004 Tobias Koenig" ) );
151 about->addAuthor( ki18n(
"Tobias Koenig" ), KLocalizedString(),
"tokoe@kde.org" );
152 about->addAuthor( ki18n(
"Cornelius Schumacher" ), KLocalizedString(),
"schumacher@kde.org" );
153 setAboutData( about );
156 void KCMDesignerFields::delayedInit()
158 kDebug() <<
"KCMDesignerFields::delayedInit()";
162 connect( mPageView, SIGNAL(itemSelectionChanged()),
163 this, SLOT(updatePreview()) );
164 connect( mPageView, SIGNAL(itemClicked(QTreeWidgetItem*,
int)),
165 this, SLOT(itemClicked(QTreeWidgetItem*)) );
167 connect( mDeleteButton, SIGNAL(clicked()),
168 this, SLOT(deleteFile()) );
169 connect( mImportButton, SIGNAL(clicked()),
170 this, SLOT(importFile()) );
171 connect( mDesignerButton, SIGNAL(clicked()),
172 this, SLOT(startDesigner()) );
177 KDirWatch *dw =
new KDirWatch(
this );
179 dw->addDir(
localUiDir(), KDirWatch::WatchFiles );
180 connect( dw, SIGNAL(created(QString)), SLOT(rebuildList()) );
181 connect( dw, SIGNAL(deleted(QString)), SLOT(rebuildList()) );
182 connect( dw, SIGNAL(dirty(QString)), SLOT(rebuildList()) );
185 void KCMDesignerFields::deleteFile()
187 foreach ( QTreeWidgetItem *item, mPageView->selectedItems() ) {
188 PageItem *pageItem =
static_cast<PageItem*
>( item->parent() ? item->parent() : item );
189 if ( KMessageBox::warningContinueCancel(
191 i18n(
"<qt>Do you really want to delete '<b>%1</b>'?</qt>",
192 pageItem->text(0) ), QString(), KStandardGuiItem::del() ) == KMessageBox::Continue ) {
193 KIO::NetAccess::del( pageItem->path(), 0 );
199 void KCMDesignerFields::importFile()
201 KUrl src = KFileDialog::getOpenFileName( QDir::homePath(),
202 i18n(
"*.ui|Designer Files" ),
203 this, i18n(
"Import Page" ) );
206 dest.setFileName( src.fileName() );
207 KIO::Job *job = KIO::file_copy( src, dest, -1, KIO::Overwrite );
208 KIO::NetAccess::synchronousRun( job,
this );
215 const QStringList list = KGlobal::dirs()->findAllResources(
"data",
uiPath() + QLatin1String(
"/*.ui"),
216 KStandardDirs::Recursive |
217 KStandardDirs::NoDuplicates );
218 for ( QStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it ) {
219 new PageItem( mPageView, *it );
223 void KCMDesignerFields::rebuildList()
237 QTreeWidgetItemIterator it( mPageView );
239 if ( (*it)->parent() == 0 ) {
240 PageItem *item =
static_cast<PageItem*
>( *it );
241 if ( ai.contains( item->name() ) ) {
242 item->setCheckState( 0, Qt::Checked );
243 item->setIsActive(
true );
262 QTreeWidgetItemIterator it( mPageView, QTreeWidgetItemIterator::Checked |
263 QTreeWidgetItemIterator::Selectable );
265 QStringList activePages;
267 if ( (*it)->parent() == 0 ) {
268 PageItem *item =
static_cast<PageItem*
>( *it );
269 activePages.append( item->name() );
287 void KCMDesignerFields::initGUI()
289 QVBoxLayout *layout =
new QVBoxLayout(
this );
290 layout->setSpacing( KDialog::spacingHint() );
291 layout->setMargin( KDialog::marginHint() );
293 bool noDesigner = KStandardDirs::findExe( QLatin1String(
"designer") ).isEmpty();
297 i18n(
"<qt><b>Warning:</b> Qt Designer could not be found. It is probably not "
298 "installed. You will only be able to import existing designer files.</qt>" );
299 QLabel *lbl =
new QLabel( txt,
this );
300 layout->addWidget( lbl );
303 QHBoxLayout *hbox =
new QHBoxLayout();
304 layout->addLayout( hbox );
305 hbox->setSpacing( KDialog::spacingHint() );
307 mPageView =
new QTreeWidget(
this );
308 mPageView->setHeaderLabel( i18n(
"Available Pages" ) );
309 mPageView->setRootIsDecorated(
true );
310 mPageView->setAllColumnsShowFocus(
true );
311 mPageView->header()->setResizeMode( QHeaderView::Stretch );
312 hbox->addWidget( mPageView );
314 QGroupBox *box =
new QGroupBox( i18n(
"Preview of Selected Page" ),
this );
315 QVBoxLayout *boxLayout =
new QVBoxLayout( box );
317 mPagePreview =
new QLabel( box );
318 mPagePreview->setMinimumWidth( 300 );
319 boxLayout->addWidget( mPagePreview );
321 mPageDetails =
new QLabel( box );
322 boxLayout->addWidget( mPageDetails );
323 boxLayout->addStretch( 1 );
325 hbox->addWidget( box );
329 hbox =
new QHBoxLayout();
330 layout->addLayout( hbox );
331 hbox->setSpacing( KDialog::spacingHint() );
334 i18n(
"<qt><p>This section allows you to add your own GUI"
335 " Elements ('<i>Widgets</i>') to store your own values"
336 " into %1. Proceed as described below:</p>"
338 "<li>Click on '<i>Edit with Qt Designer</i>'</li>"
339 "<li>In the dialog, select '<i>Widget</i>', then click <i>OK</i></li>"
340 "<li>Add your widgets to the form</li>"
341 "<li>Save the file in the directory proposed by Qt Designer</li>"
342 "<li>Close Qt Designer</li>"
344 "<p>In case you already have a designer file (*.ui) located"
345 " somewhere on your hard disk, simply choose '<i>Import Page</i>'</p>"
346 "<p><b>Important:</b> The name of each input widget you place within"
347 " the form must start with '<i>X_</i>'; so if you want the widget to"
348 " correspond to your custom entry '<i>X-Foo</i>', set the widget's"
349 " <i>name</i> property to '<i>X_Foo</i>'.</p>"
350 "<p><b>Important:</b> The widget will edit custom fields with an"
351 " application name of %2. To change the application name"
352 " to be edited, set the widget name in Qt Designer.</p></qt>",
355 QLabel *activeLabel =
new QLabel(
356 i18n(
"<a href=\"whatsthis:%1\">How does this work?</a>", cwHowto ),
this );
357 activeLabel->setTextInteractionFlags( Qt::LinksAccessibleByMouse|
358 Qt::LinksAccessibleByKeyboard );
359 connect( activeLabel, SIGNAL(linkActivated(QString)),
360 this, SLOT(showWhatsThis(QString)) );
361 hbox->addWidget( activeLabel );
364 activeLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
366 hbox->addStretch( 1 );
368 mDeleteButton =
new QPushButton( i18n(
"Delete Page" ),
this );
369 mDeleteButton->setEnabled(
false );
370 hbox->addWidget( mDeleteButton );
371 mImportButton =
new QPushButton( i18n(
"Import Page..." ),
this );
372 hbox->addWidget( mImportButton );
373 mDesignerButton =
new QPushButton( i18n(
"Edit with Qt Designer..." ),
this );
374 hbox->addWidget( mDesignerButton );
377 mDesignerButton->setEnabled(
false );
381 void KCMDesignerFields::updatePreview()
383 QTreeWidgetItem *item = 0;
384 if ( mPageView->selectedItems().size() == 1 ) {
385 item = mPageView->selectedItems().first();
387 bool widgetItemSelected =
false;
390 if ( item->parent() ) {
391 QString details = QString::fromLatin1(
"<qt><table>"
392 "<tr><td align=\"right\"><b>%1</b></td><td>%2</td></tr>"
393 "<tr><td align=\"right\"><b>%3</b></td><td>%4</td></tr>"
394 "<tr><td align=\"right\"><b>%5</b></td><td>%6</td></tr>"
395 "<tr><td align=\"right\"><b>%7</b></td><td>%8</td></tr>"
397 .arg( i18n(
"Key:" ) )
398 .arg( item->text( 0 ).replace( QLatin1String(
"X_"),QLatin1String(
"X-") ) )
399 .arg( i18n(
"Type:" ) )
400 .arg( item->text( 1 ) )
401 .arg( i18n(
"Classname:" ) )
402 .arg( item->text( 2 ) )
403 .arg( i18n(
"Description:" ) )
404 .arg( item->text( 3 ) );
406 mPageDetails->setText( details );
408 PageItem *pageItem =
static_cast<PageItem*
>( item->parent() );
409 mPagePreview->setWindowIcon( pageItem->preview() );
411 mPageDetails->setText( QString() );
413 PageItem *pageItem =
static_cast<PageItem*
>( item );
414 mPagePreview->setWindowIcon( pageItem->preview() );
416 widgetItemSelected =
true;
419 mPagePreview->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
421 mPagePreview->setWindowIcon( QPixmap() );
422 mPagePreview->setFrameStyle( 0 );
423 mPageDetails->setText( QString() );
426 mDeleteButton->setEnabled( widgetItemSelected );
429 void KCMDesignerFields::itemClicked( QTreeWidgetItem *item )
431 if ( !item || item->parent() != 0 ) {
435 PageItem *pageItem =
static_cast<PageItem*
>( item );
437 if ( pageItem->isOn() != pageItem->isActive() ) {
438 emit changed(
true );
439 pageItem->setIsActive( pageItem->isOn() );
443 void KCMDesignerFields::startDesigner()
445 QString cmdLine = QLatin1String(
"designer");
449 if( !KGlobal::dirs()->exists(cepPath) ) {
450 KIO::NetAccess::mkdir( cepPath,
this );
454 QDir::setCurrent( QLatin1String(cepPath.toLocal8Bit()) );
456 QTreeWidgetItem *item = 0;
457 if ( mPageView->selectedItems().size() == 1 ) {
458 item = mPageView->selectedItems().first();
461 PageItem *pageItem =
static_cast<PageItem*
>( item->parent() ? item->parent() : item );
462 cmdLine += QLatin1Char(
' ') + KShell::quoteArg( pageItem->path() );
465 KRun::runCommand( cmdLine, topLevelWidget() );
468 void KCMDesignerFields::showWhatsThis(
const QString &href )
470 if ( href.startsWith( QLatin1String(
"whatsthis:" ) ) ) {
471 QPoint pos = QCursor::pos();
472 QWhatsThis::showText( pos, href.mid( 10 ), this );
476 #include "kcmdesignerfields.moc"
KCMDesignerFields(const KComponentData &instance, QWidget *parent=0, const QVariantList &args=QVariantList())
virtual void writeActivePages(const QStringList &)=0
void loadActivePages(const QStringList &)
virtual QString uiPath()=0
QStringList saveActivePages()
virtual QString applicationName()=0
virtual QString localUiDir()=0
virtual QStringList readActivePages()=0