Kross
form.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * form.cpp 00003 * This file is part of the KDE project 00004 * copyright (C)2006-2007 by Sebastian Sauer (mail@dipe.org) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; see the file COPYING. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 ***************************************************************************/ 00019 00020 #include "form.h" 00021 00022 #include <QtCore/QByteRef> 00023 #include <QtCore/QBuffer> 00024 #include <QtCore/QRegExp> 00025 #include <QtCore/QFile> 00026 #include <QtCore/QArgument> 00027 #include <QtCore/QMetaEnum> 00028 #include <QtGui/QKeyEvent> 00029 #include <QtGui/QDialog> 00030 #include <QtGui/QBoxLayout> 00031 #include <QtGui/QStackedLayout> 00032 #include <QtGui/QSizePolicy> 00033 #include <QtGui/QApplication> 00034 #include <QtGui/QProgressBar> 00035 //#include <QtGui/QProgressDialog> 00036 #include <QtGui/QTextBrowser> 00037 #include <QUiLoader> 00038 #include <QTextCursor> 00039 #include <QTextBlock> 00040 00041 #include <kdebug.h> 00042 #include <klocale.h> 00043 #include <kurl.h> 00044 #include <kpushbutton.h> 00045 //#include <kurlcombobox.h> 00046 //#include <kdiroperator.h> 00047 //#include <kshell.h> 00048 #include <kicon.h> 00049 #include <kaction.h> 00050 #include <kactioncollection.h> 00051 #include <kmessagebox.h> 00052 #include <kpluginloader.h> 00053 #include <kpluginfactory.h> 00054 #include <kparts/part.h> 00055 //#include <kio/netaccess.h> 00056 //#include <klocale.h> 00057 //#include <kmimetype.h> 00058 //#include <kstandarddirs.h> 00059 #include <kfilewidget.h> 00060 #include <kurlcombobox.h> 00061 #include <kshell.h> 00062 #include <widgets/ksqueezedtextlabel.h> 00063 00064 extern "C" 00065 { 00066 KDE_EXPORT QObject* krossmodule() 00067 { 00068 return new Kross::FormModule(); 00069 } 00070 } 00071 00072 using namespace Kross; 00073 00074 /********************************************************************************* 00075 * FormList 00076 */ 00077 00078 FormListView::FormListView(QWidget* parent) : QListWidget(parent) {} 00079 FormListView::~FormListView() {} 00080 void FormListView::clear() { QListWidget::clear(); } 00081 void FormListView::remove(int index) { delete QListWidget::item(index); } 00082 void FormListView::addItem(const QString& text) { QListWidget::addItem(text); } 00083 int FormListView::count() { return QListWidget::count(); } 00084 int FormListView::current() { return QListWidget::currentRow(); } 00085 void FormListView::setCurrent(int row) { QListWidget::setCurrentRow(row); } 00086 QString FormListView::text(int row) { 00087 QListWidgetItem *item = QListWidget::item(row); 00088 return item ? item->text() : QString(); 00089 } 00090 00091 /********************************************************************************* 00092 * FormFileWidget 00093 */ 00094 00095 namespace Kross { 00096 00098 class FormFileWidget::Private 00099 { 00100 public: 00101 KFileWidget* filewidget; 00102 QString filename; 00103 }; 00104 00105 } 00106 00107 FormFileWidget::FormFileWidget(QWidget* parent, const QString& startDirOrVariable) 00108 : QWidget(parent), d(new Private()) 00109 { 00110 QVBoxLayout* layout = new QVBoxLayout(this); 00111 layout->setSpacing(0); 00112 layout->setMargin(0); 00113 setLayout(layout); 00114 00115 d->filewidget = new KFileWidget(KUrl(startDirOrVariable), this); 00116 layout->addWidget( d->filewidget ); 00117 //QMetaObject::invokeMethod(d->filewidget, "toggleSpeedbar", Q_ARG(bool,false)); 00118 //KFileDialog::setMode( KFile::File | KFile::LocalOnly ); 00119 00120 // slotOk() emits accepted, accept() emits fileSelected() 00121 QObject::connect(d->filewidget, SIGNAL(fileSelected(const QString&)), this, SLOT(slotFileSelected(const QString&))); 00122 00123 QObject::connect(d->filewidget, SIGNAL(fileSelected(const QString&)), this, SIGNAL(fileSelected(const QString&))); 00124 QObject::connect(d->filewidget, SIGNAL(fileHighlighted(const QString&)), this, SIGNAL(fileHighlighted(const QString&))); 00125 QObject::connect(d->filewidget, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged())); 00126 QObject::connect(d->filewidget, SIGNAL(filterChanged(const QString&)), this, SIGNAL(filterChanged(const QString&))); 00127 00128 // d->impl->setOperationMode(d->mode); 00129 // if( d->mimeFilter.count() > 0 ) 00130 // d->impl->setMimeFilter(d->mimeFilter); 00131 // else if( ! d->filter.isEmpty() ) 00132 // d->impl->setFilter(d->filter); 00133 00134 if( parent && parent->layout() ) 00135 parent->layout()->addWidget(this); 00136 setMinimumSize( QSize(480,360) ); 00137 } 00138 00139 FormFileWidget::~FormFileWidget() 00140 { 00141 delete d; 00142 } 00143 00144 void FormFileWidget::setMode(const QString& mode) 00145 { 00146 QMetaEnum e = metaObject()->enumerator( metaObject()->indexOfEnumerator("Mode") ); 00147 KFileWidget::OperationMode m = (KFileWidget::OperationMode) e.keysToValue( mode.toLatin1() ); 00148 d->filewidget->setOperationMode(m); 00149 } 00150 00151 QString FormFileWidget::currentFilter() const 00152 { 00153 return d->filewidget->currentFilter(); 00154 } 00155 00156 void FormFileWidget::setFilter(const QString &filter) 00157 { 00158 QString f = filter; 00159 f.replace(QRegExp("([^\\\\]{1,1})/"), "\\1\\/"); // escape '/' chars else KFileDialog assumes they are mimetypes :-/ 00160 d->filewidget->setFilter(f); 00161 } 00162 00163 QString FormFileWidget::currentMimeFilter() const 00164 { 00165 return d->filewidget->currentMimeFilter(); 00166 } 00167 00168 void FormFileWidget::setMimeFilter(const QStringList& filter) 00169 { 00170 d->filewidget->setMimeFilter(filter); 00171 } 00172 00173 void FormFileWidget::slotFileSelected( const QString & fn ) 00174 { 00175 //kDebug()<<fn; 00176 d->filename = fn; 00177 } 00178 00179 QString FormFileWidget::selectedFile() const 00180 { 00181 if ( d->filewidget->operationMode() != KFileWidget::Saving ) { 00182 d->filewidget->accept(); 00183 } else { 00184 //kDebug()<<d->filename<<d->filewidget->operationMode(); 00185 if ( d->filename.isEmpty() ) { 00186 // make KFileWidget create an url for us (including extension if necessary) 00187 QObject::connect(d->filewidget, SIGNAL(accepted()), d->filewidget, SLOT(accept())); 00188 d->filewidget->slotOk(); 00189 QObject::disconnect(d->filewidget, SIGNAL(accepted()), d->filewidget, SLOT(accept())); 00190 } 00191 } 00192 //kDebug()<<d->filename; 00193 KUrl url( d->filename ); 00194 return url.path(); // strip file:// at least python chokes on it 00195 } 00196 00197 /********************************************************************************* 00198 * FormProgressDialog 00199 */ 00200 00201 namespace Kross { 00203 class FormProgressDialog::Private 00204 { 00205 public: 00206 QTextBrowser* browser; 00207 QProgressBar* bar; 00208 bool gotCanceled; 00209 QTime time; 00210 void update() { 00211 if( time.elapsed() >= 1000 ) { 00212 time.restart(); 00213 qApp->processEvents(); 00214 } 00215 } 00216 }; 00217 } 00218 00219 FormProgressDialog::FormProgressDialog(const QString& caption, const QString& labelText) : KPageDialog(), d(new Private) 00220 { 00221 d->gotCanceled = false; 00222 d->time.start(); 00223 00224 setCaption(caption); 00225 KDialog::setButtons(KDialog::Ok|KDialog::Cancel); 00226 setFaceType(KPageDialog::Plain); 00227 enableButton(KDialog::Ok, false); 00228 //setWindowModality(Qt::WindowModal); 00229 setModal(false); //true); 00230 setMinimumWidth(540); 00231 setMinimumHeight(400); 00232 00233 QWidget* widget = new QWidget( mainWidget() ); 00234 KPageWidgetItem* item = KPageDialog::addPage(widget, QString()); 00235 item->setHeader(labelText); 00236 //item->setIcon( KIcon(iconname) ); 00237 widget = item->widget(); 00238 QVBoxLayout* layout = new QVBoxLayout(widget); 00239 layout->setMargin(0); 00240 widget->setLayout(layout); 00241 00242 d->browser = new QTextBrowser(this); 00243 d->browser->setHtml(labelText); 00244 layout->addWidget(d->browser); 00245 00246 d->bar = new QProgressBar(this); 00247 //d->bar->setFormat("%v"); 00248 d->bar->setVisible(false); 00249 layout->addWidget(d->bar); 00250 00251 setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding) ); 00252 show(); 00253 qApp->processEvents(); 00254 } 00255 00256 FormProgressDialog::~FormProgressDialog() 00257 { 00258 delete d; 00259 } 00260 00261 void FormProgressDialog::setValue(int progress) 00262 { 00263 if( progress < 0 ) { 00264 if( d->bar->isVisible() ) { 00265 d->bar->setVisible(false); 00266 d->bar->setValue(0); 00267 qApp->processEvents(); 00268 } 00269 return; 00270 } 00271 if( ! d->bar->isVisible() ) 00272 d->bar->setVisible(true); 00273 d->bar->setValue(progress); 00274 d->update(); 00275 } 00276 00277 void FormProgressDialog::setRange(int minimum, int maximum) 00278 { 00279 d->bar->setRange(minimum, maximum); 00280 } 00281 00282 void FormProgressDialog::setText(const QString& text) 00283 { 00284 d->browser->setHtml(text); 00285 d->update(); 00286 } 00287 00288 void FormProgressDialog::addText(const QString& text) 00289 { 00290 QTextCursor cursor( d->browser->document()->end() ); 00291 cursor.movePosition(QTextCursor::End); 00292 cursor.insertBlock(); 00293 cursor.insertHtml(text); 00294 d->browser->moveCursor(QTextCursor::End); 00295 d->browser->ensureCursorVisible(); 00296 d->update(); 00297 } 00298 00299 void FormProgressDialog::done(int r) 00300 { 00301 if( r == Rejected && ! d->gotCanceled ) { 00302 if( KMessageBox::messageBox(this, KMessageBox::WarningContinueCancel, i18n("Cancel?")) == KMessageBox::Continue ) { 00303 d->gotCanceled = true; 00304 enableButton(KDialog::Cancel, false); 00305 emit canceled(); 00306 } 00307 return; 00308 } 00309 KPageDialog::done(r); 00310 } 00311 00312 int FormProgressDialog::exec() 00313 { 00314 enableButton(KDialog::Ok, true); 00315 enableButton(KDialog::Cancel, false); 00316 if( d->bar->isVisible() ) 00317 d->bar->setValue( d->bar->maximum() ); 00318 return KDialog::exec(); 00319 } 00320 00321 bool FormProgressDialog::isCanceled() 00322 { 00323 return d->gotCanceled; 00324 } 00325 00326 /********************************************************************************* 00327 * FormDialog 00328 */ 00329 00330 namespace Kross { 00331 00333 class FormDialog::Private 00334 { 00335 public: 00336 KDialog::ButtonCode buttoncode; 00337 QHash<QString, KPageWidgetItem*> items; 00338 }; 00339 00340 } 00341 00342 FormDialog::FormDialog(const QString& caption) 00343 : KPageDialog( /*0, Qt::WShowModal | Qt::WDestructiveClose*/ ) 00344 , d( new Private() ) 00345 { 00346 setCaption(caption); 00347 KDialog::setButtons(KDialog::Ok); 00348 setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding) ); 00349 00350 connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), 00351 this, SLOT(slotCurrentPageChanged(KPageWidgetItem*))); 00352 } 00353 00354 FormDialog::~FormDialog() 00355 { 00356 kWarning()<<"dtor"; 00357 delete d; 00358 } 00359 00360 bool FormDialog::setButtons(const QString& buttons) 00361 { 00362 int i = metaObject()->indexOfEnumerator("ButtonCode"); 00363 Q_ASSERT( i >= 0 ); 00364 QMetaEnum e = metaObject()->enumerator(i); 00365 int v = e.keysToValue( buttons.toUtf8() ); 00366 if( v < 0 ) 00367 return false; 00368 KDialog::setButtons( (KDialog::ButtonCode) v ); 00369 return true; 00370 } 00371 00372 bool FormDialog::setButtonText(const QString& button, const QString& text) 00373 { 00374 int i = metaObject()->indexOfEnumerator("ButtonCode"); 00375 Q_ASSERT( i >= 0 ); 00376 QMetaEnum e = metaObject()->enumerator(i); 00377 int v = e.keysToValue( button.toUtf8() ); 00378 if( v < 0 ) 00379 return false; 00380 KDialog::setButtonText( (KDialog::ButtonCode) v, text); 00381 return true; 00382 } 00383 00384 bool FormDialog::setFaceType(const QString& facetype) 00385 { 00386 int i = KPageView::staticMetaObject.indexOfEnumerator("FaceType"); 00387 Q_ASSERT( i >= 0 ); 00388 QMetaEnum e = KPageView::staticMetaObject.enumerator(i); 00389 int v = e.keysToValue( facetype.toUtf8() ); 00390 if( v < 0 ) 00391 return false; 00392 KPageDialog::setFaceType( (KPageDialog::FaceType) v ); 00393 return true; 00394 } 00395 00396 QString FormDialog::currentPage() const 00397 { 00398 KPageWidgetItem* item = KPageDialog::currentPage(); 00399 return item ? item->name() : QString(); 00400 } 00401 00402 bool FormDialog::setCurrentPage(const QString& name) 00403 { 00404 if( ! d->items.contains(name) ) 00405 return false; 00406 KPageDialog::setCurrentPage( d->items[name] ); 00407 return true; 00408 } 00409 00410 QWidget* FormDialog::page(const QString& name) const 00411 { 00412 return d->items.contains(name) ? d->items[name]->widget() : 0; 00413 } 00414 00415 //shared by FormDialog and FormAssistant 00416 static KPageWidgetItem* formAddPage(KPageDialog* dialog, const QString& name, const QString& header, const QString& iconname) 00417 { 00418 QWidget* widget = new QWidget( dialog->mainWidget() ); 00419 QVBoxLayout* boxlayout = new QVBoxLayout(widget); 00420 boxlayout->setSpacing(0); 00421 boxlayout->setMargin(0); 00422 widget->setLayout(boxlayout); 00423 00424 KPageWidgetItem* item = dialog->addPage(widget, name); 00425 item->setHeader(header.isNull() ? name : header); 00426 if( ! iconname.isEmpty() ) 00427 item->setIcon( KIcon(iconname) ); 00428 //d->items.insert(name, item); 00429 00430 return item; 00431 } 00432 00433 QWidget* FormDialog::addPage(const QString& name, const QString& header, const QString& iconname) 00434 { 00435 return d->items.insert(name, formAddPage((KPageDialog*)this,name,header,iconname)).value()->widget(); 00436 } 00437 00438 void FormDialog::setMainWidget(QWidget *newMainWidget) 00439 { 00440 KDialog::setMainWidget(newMainWidget); 00441 } 00442 00443 QString FormDialog::result() 00444 { 00445 int i = metaObject()->indexOfEnumerator("ButtonCode"); 00446 if( i < 0 ) { 00447 kWarning() << "Kross::FormDialog::setButtons No such enumerator \"ButtonCode\""; 00448 return QString(); 00449 } 00450 QMetaEnum e = metaObject()->enumerator(i); 00451 return e.valueToKey(d->buttoncode); 00452 } 00453 00454 void FormDialog::slotButtonClicked(int button) 00455 { 00456 d->buttoncode = (KDialog::ButtonCode) button; 00457 KDialog::slotButtonClicked(button); 00458 } 00459 00460 void FormDialog::slotCurrentPageChanged(KPageWidgetItem* current) 00461 { 00462 Q_UNUSED(current); 00463 //kDebug() << "FormDialog::slotCurrentPageChanged current=" << current->name(); 00464 //foreach(QWidget* widget, current->widget()->findChildren< QWidget* >("")) widget->setFocus(); 00465 } 00466 00467 00468 namespace Kross { 00470 class FormAssistant::Private 00471 { 00472 public: 00473 KDialog::ButtonCode buttoncode; 00474 QHash<QString, KPageWidgetItem*> items; 00475 }; 00476 } 00477 FormAssistant::FormAssistant(const QString& caption) 00478 : KAssistantDialog( /*0, Qt::WShowModal | Qt::WDestructiveClose*/ ) 00479 , d( new Private() ) 00480 { 00481 setCaption(caption); 00482 setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding) ); 00483 00484 connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), 00485 this, SLOT(slotCurrentPageChanged(KPageWidgetItem*))); 00486 /* unlike boost qt does not support defining of slot call order! 00487 connect(this, SIGNAL(user2Clicked()), this, SIGNAL (nextClicked())); 00488 connect(this, SIGNAL(user3Clicked()), this, SIGNAL (backClicked())); 00489 */ 00490 } 00491 00492 FormAssistant::~FormAssistant() 00493 { 00494 delete d; 00495 } 00496 00497 void FormAssistant::showHelpButton(bool show) 00498 { 00499 showButton(KDialog::Help, show); 00500 } 00501 00502 void FormAssistant::back() 00503 { 00504 emit backClicked(); 00505 KAssistantDialog::back(); 00506 } 00507 void FormAssistant::next() 00508 { 00509 emit nextClicked(); 00510 KAssistantDialog::next(); 00511 } 00512 00513 QString FormAssistant::currentPage() const 00514 { 00515 KPageWidgetItem* item = KPageDialog::currentPage(); 00516 return item ? item->name() : QString(); 00517 } 00518 00519 bool FormAssistant::setCurrentPage(const QString& name) 00520 { 00521 if( ! d->items.contains(name) ) 00522 return false; 00523 KPageDialog::setCurrentPage( d->items[name] ); 00524 return true; 00525 } 00526 00527 QWidget* FormAssistant::page(const QString& name) const 00528 { 00529 return d->items.contains(name) ? d->items[name]->widget() : 0; 00530 } 00531 00532 QWidget* FormAssistant::addPage(const QString& name, const QString& header, const QString& iconname) 00533 { 00534 return d->items.insert(name, formAddPage((KPageDialog*)this,name,header,iconname)).value()->widget(); 00535 } 00536 00537 bool FormAssistant::isAppropriate (const QString& name) const 00538 { 00539 return d->items.contains(name) && KAssistantDialog::isAppropriate(d->items[name]); 00540 } 00541 void FormAssistant::setAppropriate (const QString& name, bool appropriate) 00542 { 00543 if (!d->items.contains(name)) 00544 return; 00545 00546 KAssistantDialog::setAppropriate(d->items[name],appropriate); 00547 } 00548 bool FormAssistant::isValid (const QString& name) const 00549 { 00550 return d->items.contains(name) && KAssistantDialog::isValid(d->items[name]); 00551 } 00552 void FormAssistant::setValid (const QString& name, bool enable) 00553 { 00554 if (!d->items.contains(name)) 00555 return; 00556 00557 KAssistantDialog::setValid(d->items[name],enable); 00558 } 00559 00560 QString FormAssistant::result() 00561 { 00562 int i = metaObject()->indexOfEnumerator("AssistantButtonCode"); 00563 if( i < 0 ) { 00564 kWarning() << "Kross::FormAssistant::setButtons No such enumerator \"AssistantButtonCode\""; 00565 return QString(); 00566 } 00567 QMetaEnum e = metaObject()->enumerator(i); 00568 return e.valueToKey(FormAssistant::AssistantButtonCode(int(d->buttoncode))); 00569 } 00570 00571 void FormAssistant::slotButtonClicked(int button) 00572 { 00573 d->buttoncode = (KDialog::ButtonCode) button; 00574 KDialog::slotButtonClicked(button); 00575 } 00576 00577 void FormAssistant::slotCurrentPageChanged(KPageWidgetItem* current) 00578 { 00579 Q_UNUSED(current); 00580 //kDebug() << "FormAssistant::slotCurrentPageChanged current=" << current->name(); 00581 //foreach(QWidget* widget, current->widget()->findChildren< QWidget* >("")) widget->setFocus(); 00582 } 00583 00584 /********************************************************************************* 00585 * FormModule 00586 */ 00587 00588 namespace Kross { 00589 00591 class UiLoader : public QUiLoader 00592 { 00593 public: 00594 UiLoader() : QUiLoader() {} 00595 virtual ~UiLoader() {} 00596 00597 /* 00598 virtual QAction* createAction(QObject* parent = 0, const QString& name = QString()) 00599 { 00600 } 00601 00602 virtual QActionGroup* createActionGroup(QObject* parent = 0, const QString& name = QString()) 00603 { 00604 } 00605 00606 virtual QLayout* createLayout(const QString& className, QObject* parent = 0, const QString& name = QString()) 00607 { 00608 } 00609 00610 virtual QWidget* createWidget(const QString& className, QWidget* parent = 0, const QString& name = QString()) 00611 { 00612 } 00613 */ 00614 }; 00615 00617 class FormModule::Private 00618 { 00619 public: 00620 }; 00621 00622 } 00623 00624 FormModule::FormModule() 00625 : QObject() 00626 , d( new Private() ) 00627 { 00628 } 00629 00630 FormModule::~FormModule() 00631 { 00632 delete d; 00633 } 00634 00635 QWidget* FormModule::activeModalWidget() 00636 { 00637 return QApplication::activeModalWidget(); 00638 } 00639 00640 QWidget* FormModule::activeWindow() 00641 { 00642 return QApplication::activeWindow(); 00643 } 00644 00645 QString FormModule::showMessageBox(const QString& dialogtype, const QString& caption, const QString& message, const QString& details) 00646 { 00647 KMessageBox::DialogType type; 00648 if(dialogtype == "Error") { 00649 if( ! details.isNull() ) { 00650 KMessageBox::detailedError(0, message, details, caption); 00651 return QString(); 00652 } 00653 type = KMessageBox::Error; 00654 } 00655 else if(dialogtype == "Sorry") { 00656 if( ! details.isNull() ) { 00657 KMessageBox::detailedSorry(0, message, details, caption); 00658 return QString(); 00659 } 00660 type = KMessageBox::Sorry; 00661 } 00662 else if(dialogtype == "QuestionYesNo") type = KMessageBox::QuestionYesNo; 00663 else if(dialogtype == "WarningYesNo") type = KMessageBox::WarningYesNo; 00664 else if(dialogtype == "WarningContinueCancel") type = KMessageBox::WarningContinueCancel; 00665 else if(dialogtype == "WarningYesNoCancel") type = KMessageBox::WarningYesNoCancel; 00666 else if(dialogtype == "QuestionYesNoCancel") type = KMessageBox::QuestionYesNoCancel; 00667 else /*if(dialogtype == "Information")*/ type = KMessageBox::Information; 00668 switch( KMessageBox::messageBox(0, type, message, caption) ) { 00669 case KMessageBox::Ok: return "Ok"; 00670 case KMessageBox::Cancel: return "Cancel"; 00671 case KMessageBox::Yes: return "Yes"; 00672 case KMessageBox::No: return "No"; 00673 case KMessageBox::Continue: return "Continue"; 00674 default: break; 00675 } 00676 return QString(); 00677 } 00678 00679 QWidget* FormModule::showProgressDialog(const QString& caption, const QString& labelText) 00680 { 00681 return new FormProgressDialog(caption, labelText); 00682 } 00683 00684 QWidget* FormModule::createDialog(const QString& caption) 00685 { 00686 return new FormDialog(caption); 00687 } 00688 00689 QWidget* FormModule::createAssistant(const QString& caption) 00690 { 00691 return new FormAssistant(caption); 00692 } 00693 00694 QObject* FormModule::createLayout(QWidget* parent, const QString& layout) 00695 { 00696 QLayout* l = 0; 00697 if( layout == "QVBoxLayout" ) 00698 l = new QVBoxLayout(); 00699 else if( layout == "QHBoxLayout" ) 00700 l = new QHBoxLayout(); 00701 else if( layout == "QStackedLayout" ) 00702 l = new QStackedLayout(); 00703 if( parent && l ) 00704 parent->setLayout(l); 00705 return l; 00706 } 00707 00708 QWidget* FormModule::createWidget(const QString& className) 00709 { 00710 UiLoader loader; 00711 QWidget* widget = loader.createWidget(className); 00712 return widget; 00713 } 00714 00715 QWidget* FormModule::createWidget(QWidget* parent, const QString& className, const QString& name) 00716 { 00717 UiLoader loader; 00718 QWidget* widget = loader.createWidget(className, parent, name); 00719 if( parent && parent->layout() ) 00720 parent->layout()->addWidget(widget); 00721 return widget; 00722 } 00723 00724 00725 QString FormModule::tr(const QString& str) 00726 { 00727 return QObject::tr(str.toUtf8()); 00728 } 00729 QString FormModule::tr(const QString& str, const QString& comment) 00730 { 00731 return QObject::tr(str.toUtf8(),comment.toUtf8()); 00732 } 00733 00734 QWidget* FormModule::createWidgetFromUI(QWidget* parent, const QString& xml) 00735 { 00736 QUiLoader loader; 00737 00738 QDomDocument doc("mydocument"); 00739 doc.setContent(xml.toUtf8()); 00740 00741 QDomNodeList strings=doc.elementsByTagName("string"); 00742 int i=strings.size(); 00743 while(--i>=0) 00744 { 00745 QDomElement e=strings.at(i).toElement(); 00746 QString i18nd=e.attribute("comment").isEmpty()?QObject::tr(e.text().toUtf8()):QObject::tr(e.text().toUtf8(),e.attribute("comment").toUtf8()); 00747 if (i18nd==e.text()) 00748 continue; 00749 QDomNode n = e.firstChild(); 00750 while (!n.isNull()) 00751 { 00752 QDomNode nn=n.nextSibling(); 00753 if (n.isCharacterData()) 00754 e.removeChild(n); 00755 n = nn; 00756 } 00757 e.appendChild(e.ownerDocument().createTextNode(i18nd)); 00758 } 00759 00760 QByteArray ba = doc.toByteArray(); 00761 QBuffer buffer(&ba); 00762 buffer.open(QIODevice::ReadOnly); 00763 00764 QWidget* widget = loader.load(&buffer, parent); 00765 if( widget && parent && parent->layout() ) 00766 parent->layout()->addWidget(widget); 00767 return widget; 00768 } 00769 00770 QWidget* FormModule::createWidgetFromUIFile(QWidget* parent, const QString& filename) 00771 { 00772 QFile file(filename); 00773 if( ! file.exists() ) { 00774 kDebug() << QString("Kross::FormModule::createWidgetFromUIFile: There exists no such file \"%1\"").arg(filename); 00775 return false; 00776 } 00777 if( ! file.open(QFile::ReadOnly) ) { 00778 kDebug() << QString("Kross::FormModule::createWidgetFromUIFile: Failed to open the file \"%1\"").arg(filename); 00779 return false; 00780 } 00781 const QString xml = file.readAll(); 00782 file.close(); 00783 return createWidgetFromUI(parent, xml); 00784 } 00785 00786 QWidget* FormModule::createFileWidget(QWidget* parent, const QString& startDirOrVariable) 00787 { 00788 FormFileWidget* widget = new FormFileWidget(parent, startDirOrVariable); 00789 if( parent && parent->layout() ) 00790 parent->layout()->addWidget(widget); 00791 return widget; 00792 } 00793 00794 QWidget* FormModule::createListView(QWidget* parent) 00795 { 00796 FormListView* widget = new FormListView(parent); 00797 if( parent && parent->layout() ) 00798 parent->layout()->addWidget(widget); 00799 return widget; 00800 } 00801 00802 QAction* FormModule::createAction(QObject* parent) 00803 { 00804 return new QAction(parent); 00805 } 00806 00807 QObject* FormModule::loadPart(QWidget* parent, const QString& name, const QUrl& url) 00808 { 00809 //name e.g. "libkghostview" 00810 KPluginFactory* factory = KPluginLoader( name.toLatin1() ).factory(); 00811 if( ! factory ) { 00812 kWarning() << QString("Kross::FormModule::loadPart: No such library \"%1\"").arg(name); 00813 return 0; 00814 } 00815 KParts::ReadOnlyPart* part = factory->create< KParts::ReadOnlyPart >( parent ); 00816 if( ! part ) { 00817 kWarning() << QString("Kross::FormModule::loadPart: Library \"%1\" is not a KPart").arg(name); 00818 return 0; 00819 } 00820 if( url.isValid() ) 00821 part->openUrl(url); 00822 if( parent && parent->layout() && part->widget() ) 00823 parent->layout()->addWidget( part->widget() ); 00824 return part; 00825 } 00826 00827 #include "form.moc"
KDE 4.6 API Reference