00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "qdesigner_resource.h"
00025 #include "formwindow.h"
00026 #include "qdesigner_tabwidget_p.h"
00027 #include "qdesigner_toolbox_p.h"
00028 #include "qdesigner_stackedbox_p.h"
00029 #include "qdesigner_toolbar_p.h"
00030 #include "qdesigner_dockwidget_p.h"
00031 #include "qdesigner_menu_p.h"
00032 #include "qdesigner_menubar_p.h"
00033 #include "ui4_p.h"
00034
00035
00036 #include <widgetdatabase_p.h>
00037 #include <layout_p.h>
00038 #include <layoutinfo_p.h>
00039 #include <spacer_widget_p.h>
00040 #include <resourcefile_p.h>
00041 #include <pluginmanager_p.h>
00042 #include <metadatabase_p.h>
00043 #include <widgetfactory_p.h>
00044 #include <abstractlanguage.h>
00045
00046 #include <qdesigner_widget_p.h>
00047 #include <qlayout_widget_p.h>
00048 #include <qdesigner_promotedwidget_p.h>
00049 #include <qdesigner_utils_p.h>
00050 #include <ui4_p.h>
00051
00052
00053 #include <QtDesigner/QtDesigner>
00054
00055 #include <QtGui/QMenu>
00056 #include <QtGui/QMessageBox>
00057 #include <QtGui/QLayout>
00058 #include <QtGui/QTabWidget>
00059 #include <QtGui/QToolBox>
00060 #include <QtGui/QTabBar>
00061 #include <QtGui/QAction>
00062 #include <QtGui/QActionGroup>
00063 #include <QtGui/QApplication>
00064 #include <QtGui/QMainWindow>
00065 #include <QtGui/QSplitter>
00066 #include <QtGui/QMenuBar>
00067 #include <QtGui/QFileDialog>
00068
00069 #include <QtCore/QBuffer>
00070 #include <QtCore/QDir>
00071 #include <QtCore/QProcess>
00072 #include <QtCore/QLibraryInfo>
00073 #include <QtCore/QMetaProperty>
00074 #include <QtCore/qdebug.h>
00075
00076 #include <QtXml/QDomDocument>
00077
00078
00079 using namespace qdesigner_internal;
00080
00081 QDesignerResource::QDesignerResource(FormWindow *formWindow)
00082 : QSimpleResource(formWindow->core()), m_formWindow(formWindow)
00083 {
00084 setWorkingDirectory(formWindow->absoluteDir());
00085
00086 m_topLevelSpacerCount = 0;
00087 m_copyWidget = false;
00088 m_selected = 0;
00089
00090
00091 m_internal_to_qt.insert(QLatin1String("QLayoutWidget"), QLatin1String("QWidget"));
00092 m_internal_to_qt.insert(QLatin1String("QDesignerWidget"), QLatin1String("QWidget"));
00093 m_internal_to_qt.insert(QLatin1String("QDesignerStackedWidget"), QLatin1String("QStackedWidget"));
00094 m_internal_to_qt.insert(QLatin1String("QDesignerTabWidget"), QLatin1String("QTabWidget"));
00095 m_internal_to_qt.insert(QLatin1String("QDesignerDialog"), QLatin1String("QDialog"));
00096 m_internal_to_qt.insert(QLatin1String("QDesignerLabel"), QLatin1String("QLabel"));
00097 m_internal_to_qt.insert(QLatin1String("QDesignerToolBox"), QLatin1String("QToolBox"));
00098 m_internal_to_qt.insert(QLatin1String("QDesignerToolBar"), QLatin1String("QToolBar"));
00099 m_internal_to_qt.insert(QLatin1String("QDesignerMenuBar"), QLatin1String("QMenuBar"));
00100 m_internal_to_qt.insert(QLatin1String("QDesignerMenu"), QLatin1String("QMenu"));
00101 m_internal_to_qt.insert(QLatin1String("QDesignerDockWidget"), QLatin1String("QDockWidget"));
00102 m_internal_to_qt.insert(QLatin1String("QDesignerQ3WidgetStack"), QLatin1String("Q3WidgetStack"));
00103
00104
00105 QHashIterator<QString, QString> it(m_internal_to_qt);
00106 while (it.hasNext()) {
00107 it.next();
00108
00109 if (it.value() == QLatin1String("QDesignerWidget")
00110 || it.value() == QLatin1String("QLayoutWidget"))
00111 continue;
00112
00113 m_qt_to_internal.insert(it.value(), it.key());
00114 }
00115 }
00116
00117 QDesignerResource::~QDesignerResource()
00118 {
00119 }
00120
00121 void QDesignerResource::save(QIODevice *dev, QWidget *widget)
00122 {
00123 m_topLevelSpacerCount = 0;
00124
00125 QAbstractFormBuilder::save(dev, widget);
00126
00127 if (m_topLevelSpacerCount != 0) {
00128 QMessageBox::warning(widget->window(), QApplication::translate("Designer", "Qt Designer"),
00129 QApplication::translate("Designer", "This file contains top level spacers.<br>"
00130 "They have <b>NOT</b> been saved into the form.<br>"
00131 "Perhaps you forgot to create a layout?"),
00132 QMessageBox::Ok, 0);
00133 }
00134 }
00135
00136 void QDesignerResource::saveDom(DomUI *ui, QWidget *widget)
00137 {
00138 QAbstractFormBuilder::saveDom(ui, widget);
00139
00140 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), widget);
00141 Q_ASSERT(sheet != 0);
00142
00143 ui->setElementClass(sheet->property(sheet->indexOf(QLatin1String("objectName"))).toString());
00144
00145 for (int index = 0; index < m_formWindow->toolCount(); ++index) {
00146 QDesignerFormWindowToolInterface *tool = m_formWindow->tool(index);
00147 Q_ASSERT(tool != 0);
00148 tool->saveToDom(ui, widget);
00149 }
00150
00151 QString author = m_formWindow->author();
00152 if (!author.isEmpty()) {
00153 ui->setElementAuthor(author);
00154 }
00155
00156 QString comment = m_formWindow->comment();
00157 if (!comment.isEmpty()) {
00158 ui->setElementComment(comment);
00159 }
00160
00161 QString exportMacro = m_formWindow->exportMacro();
00162 if (!exportMacro.isEmpty()) {
00163 ui->setElementExportMacro(exportMacro);
00164 }
00165
00166 if (!m_formWindow->includeHints().isEmpty()) {
00167 QList<DomInclude*> ui_includes;
00168 foreach (QString includeHint, m_formWindow->includeHints()) {
00169 if (includeHint.isEmpty())
00170 continue;
00171
00172 DomInclude *incl = new DomInclude;
00173 QString location = QLatin1String("local");
00174 if (includeHint.at(0) == QLatin1Char('<'))
00175 location = QLatin1String("global");
00176
00177 includeHint = includeHint
00178 .replace(QLatin1Char('"'), "")
00179 .replace(QLatin1Char('<'), "")
00180 .replace(QLatin1Char('>'), "");
00181
00182 incl->setAttributeLocation(location);
00183 incl->setText(includeHint);
00184 ui_includes.append(incl);
00185 }
00186
00187 DomIncludes *includes = new DomIncludes;
00188 includes->setElementInclude(ui_includes);
00189 ui->setElementIncludes(includes);
00190 }
00191
00192 int defaultMargin = INT_MIN, defaultSpacing = INT_MIN;
00193 m_formWindow->layoutDefault(&defaultMargin, &defaultSpacing);
00194
00195 if (defaultMargin != INT_MIN || defaultSpacing != INT_MIN) {
00196 DomLayoutDefault *def = new DomLayoutDefault;
00197 if (defaultMargin != INT_MIN)
00198 def->setAttributeMargin(defaultMargin);
00199 if (defaultSpacing != INT_MIN)
00200 def->setAttributeSpacing(defaultSpacing);
00201 ui->setElementLayoutDefault(def);
00202 }
00203
00204 QString marginFunction, spacingFunction;
00205 m_formWindow->layoutFunction(&marginFunction, &spacingFunction);
00206 if (!marginFunction.isEmpty() || !spacingFunction.isEmpty()) {
00207 DomLayoutFunction *def = new DomLayoutFunction;
00208
00209 if (!marginFunction.isEmpty())
00210 def->setAttributeMargin(marginFunction);
00211 if (!spacingFunction.isEmpty())
00212 def->setAttributeSpacing(spacingFunction);
00213 ui->setElementLayoutFunction(def);
00214 }
00215
00216 QString pixFunction = m_formWindow->pixmapFunction();
00217 if (!pixFunction.isEmpty()) {
00218 ui->setElementPixmapFunction(pixFunction);
00219 }
00220 }
00221
00222 QWidget *QDesignerResource::create(DomUI *ui, QWidget *parentWidget)
00223 {
00224 QString version = ui->attributeVersion();
00225 if (version != QLatin1String("4.0")) {
00226
00227
00228 QProcess uic3;
00229 uic3.start(QLibraryInfo::location(QLibraryInfo::BinariesPath)
00230 + QDir::separator() + QLatin1String("uic3"), QStringList()
00231 << QLatin1String("-convert") << m_formWindow->fileName());
00232 bool allOK = uic3.waitForFinished();
00233 if (allOK) {
00234 QByteArray ba = uic3.readAll();
00235 QBuffer buffer(&ba);
00236 m_formWindow->setFileName(QString());
00237 QWidget *w = load(&buffer, parentWidget);
00238 if (!w) {
00239 allOK = false;
00240 } else {
00241 QMessageBox::information(parentWidget->window(), QApplication::translate("Designer", "Qt Designer"),
00242 QApplication::translate("Designer", "This file was created using Designer from Qt-%1 and"
00243 " will be converted to a new form by Qt Designer.\n"
00244 "The old form has been untouched, but you will have to save this form"
00245 " under a new name.").arg(version), QMessageBox::Ok, 0);
00246 return w;
00247 }
00248 }
00249
00250 if (!allOK) {
00251 QMessageBox::warning(parentWidget->window(), QApplication::translate("Designer", "Qt Designer"),
00252 QApplication::translate("Designer", "This file was created using designer from Qt-%1 and "
00253 "could not be read. "
00254 "Please run it through <b>uic3 -convert</b> to convert "
00255 "it to Qt-4's ui format.").arg(version),
00256 QMessageBox::Ok, 0);
00257 return 0;
00258 }
00259 }
00260
00261 qdesigner_internal::WidgetFactory *factory = qobject_cast<qdesigner_internal::WidgetFactory*>(core()->widgetFactory());
00262 Q_ASSERT(factory != 0);
00263
00264 QDesignerFormWindowInterface *previousFormWindow = factory->currentFormWindow(m_formWindow);
00265
00266 m_isMainWidget = true;
00267 QWidget *mainWidget = QAbstractFormBuilder::create(ui, parentWidget);
00268
00269 if (mainWidget && m_formWindow) {
00270 m_formWindow->setAuthor(ui->elementAuthor());
00271 m_formWindow->setComment(ui->elementComment());
00272 m_formWindow->setExportMacro(ui->elementExportMacro());
00273 m_formWindow->setPixmapFunction(ui->elementPixmapFunction());
00274
00275 if (DomLayoutDefault *def = ui->elementLayoutDefault()) {
00276 m_formWindow->setLayoutDefault(def->attributeMargin(), def->attributeSpacing());
00277 }
00278
00279 if (DomLayoutFunction *fun = ui->elementLayoutFunction()) {
00280 m_formWindow->setLayoutFunction(fun->attributeMargin(), fun->attributeSpacing());
00281 }
00282
00283 if (DomIncludes *includes = ui->elementIncludes()) {
00284 QStringList includeHints;
00285 foreach (DomInclude *incl, includes->elementInclude()) {
00286 QString text = incl->text();
00287
00288 if (text.isEmpty())
00289 continue;
00290
00291 if (incl->hasAttributeLocation() && incl->attributeLocation() == QLatin1String("global")) {
00292 text = text.prepend('<').append('>');
00293 } else {
00294 text = text.prepend('"').append('"');
00295 }
00296
00297 includeHints.append(text);
00298 }
00299
00300 m_formWindow->setIncludeHints(includeHints);
00301 }
00302
00303 for (int index = 0; index < m_formWindow->toolCount(); ++index) {
00304 QDesignerFormWindowToolInterface *tool = m_formWindow->tool(index);
00305 Q_ASSERT(tool != 0);
00306 tool->loadFromDom(ui, mainWidget);
00307 }
00308 }
00309
00310 factory->currentFormWindow(previousFormWindow);
00311
00312 return mainWidget;
00313 }
00314
00315 QWidget *QDesignerResource::create(DomWidget *ui_widget, QWidget *parentWidget)
00316 {
00317 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(parentWidget))
00318 parentWidget = promoted->child();
00319
00320 QString className = ui_widget->attributeClass();
00321 if (!m_isMainWidget && className == QLatin1String("QWidget") && ui_widget->elementLayout().size() &&
00322 !ui_widget->hasAttributeNative()) {
00323
00324
00325 QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget);
00326
00327 if (container == 0) {
00328
00329 ui_widget->setAttributeClass(QLatin1String("QLayoutWidget"));
00330 }
00331 }
00332
00333
00334 QList<DomActionRef*> actionRefs = ui_widget->elementAddAction();
00335 ui_widget->setElementAddAction(QList<DomActionRef*>());
00336
00337 QWidget *w = QAbstractFormBuilder::create(ui_widget, parentWidget);
00338
00339
00340 ui_widget->setElementAddAction(actionRefs);
00341
00342 if (w == 0)
00343 return 0;
00344
00345
00346 QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(w);
00347 QDesignerMenuBar *menuBar = qobject_cast<QDesignerMenuBar*>(w);
00348 QDesignerToolBar *toolBar = qobject_cast<QDesignerToolBar*>(w);
00349
00350 if (menu) {
00351 menu->interactive(false);
00352 menu->hide();
00353 } else if (menuBar) {
00354 menuBar->interactive(false);
00355 } else if (toolBar) {
00356 toolBar->interactive(false);
00357 }
00358
00359 foreach (DomActionRef *ui_action_ref, actionRefs) {
00360 QString name = ui_action_ref->attributeName();
00361 if (name == QLatin1String("separator")) {
00362 QAction *sep = new QAction(w);
00363 sep->setSeparator(true);
00364 w->addAction(sep);
00365 addMenuAction(sep);
00366 } else if (QAction *a = m_actions.value(name)) {
00367 w->addAction(a);
00368 } else if (QActionGroup *g = m_actionGroups.value(name)) {
00369 w->addActions(g->actions());
00370 } else if (QMenu *menu = qFindChild<QMenu*>(w, name)) {
00371 w->addAction(menu->menuAction());
00372 addMenuAction(menu->menuAction());
00373 }
00374 }
00375
00376 if (menu) {
00377 menu->interactive(true);
00378 menu->adjustSpecialActions();
00379 } else if (menuBar) {
00380 menuBar->interactive(true);
00381 menuBar->adjustSpecialActions();
00382 } else if (toolBar) {
00383 toolBar->interactive(true);
00384 toolBar->adjustSpecialActions();
00385 }
00386
00387
00388 ui_widget->setAttributeClass(className);
00389
00390 if (QDesignerExtraInfoExtension *extra = qt_extension<QDesignerExtraInfoExtension*>(core()->extensionManager(), w)) {
00391 extra->loadWidgetExtraInfo(ui_widget);
00392 }
00393
00394 return w;
00395 }
00396
00397 QLayout *QDesignerResource::create(DomLayout *ui_layout, QLayout *layout, QWidget *parentWidget)
00398 {
00399 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(parentWidget))
00400 parentWidget = promoted->child();
00401
00402 QLayout *l = QAbstractFormBuilder::create(ui_layout, layout, parentWidget);
00403
00404 if (QGridLayout *gridLayout = qobject_cast<QGridLayout*>(l))
00405 QLayoutSupport::createEmptyCells(gridLayout);
00406
00407 return l;
00408 }
00409
00410 QLayoutItem *QDesignerResource::create(DomLayoutItem *ui_layoutItem, QLayout *layout, QWidget *parentWidget)
00411 {
00412 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(parentWidget))
00413 parentWidget = promoted->child();
00414
00415 if (ui_layoutItem->kind() == DomLayoutItem::Spacer) {
00416 QHash<QString, DomProperty*> properties = propertyMap(ui_layoutItem->elementSpacer()->elementProperty());
00417
00418 Spacer *spacer = (Spacer*) core()->widgetFactory()->createWidget(QLatin1String("Spacer"), parentWidget);
00419 core()->metaDataBase()->add(spacer);
00420
00421 spacer->setInteraciveMode(false);
00422 applyProperties(spacer, ui_layoutItem->elementSpacer()->elementProperty());
00423 spacer->setInteraciveMode(true);
00424
00425 if (m_formWindow) {
00426 m_formWindow->manageWidget(spacer);
00427 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), spacer))
00428 sheet->setChanged(sheet->indexOf(QLatin1String("orientation")), true);
00429 }
00430
00431 return new QWidgetItem(spacer);
00432 } else if (ui_layoutItem->kind() == DomLayoutItem::Layout && parentWidget) {
00433 DomLayout *ui_layout = ui_layoutItem->elementLayout();
00434 QLayoutWidget *layoutWidget = new QLayoutWidget(m_formWindow, parentWidget);
00435 core()->metaDataBase()->add(layoutWidget);
00436 applyProperties(layoutWidget, ui_layout->elementProperty());
00437
00438 if (m_formWindow) {
00439 m_formWindow->manageWidget(layoutWidget);
00440 }
00441
00442 (void) create(ui_layout, 0, layoutWidget);
00443 return new QWidgetItem(layoutWidget);
00444 }
00445 return QAbstractFormBuilder::create(ui_layoutItem, layout, parentWidget);
00446 }
00447
00448 void QDesignerResource::changeObjectName(QObject *o, QString objName)
00449 {
00450 m_formWindow->unify(o, objName, true);
00451
00452 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(o)) {
00453 if (objName.startsWith(QLatin1String("__qt__promoted_"))) {
00454 promoted->setObjectName(objName);
00455 promoted->child()->setObjectName(objName.mid(15));
00456 } else {
00457 promoted->child()->setObjectName(objName);
00458 promoted->setObjectName(QLatin1String("__qt__promoted_") + objName);
00459 }
00460 } else {
00461 o->setObjectName(objName);
00462 }
00463
00464 }
00465
00466 void QDesignerResource::applyProperties(QObject *o, const QList<DomProperty*> &properties)
00467 {
00468 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), o)) {
00469
00470 for (int i=0; i<properties.size(); ++i) {
00471 DomProperty *p = properties.at(i);
00472 QString propertyName = p->attributeName();
00473
00474 int index = sheet->indexOf(propertyName);
00475 if (index != -1) {
00476 QObject *realObject = o;
00477 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(o)) {
00478 realObject = promoted->child();
00479 }
00480 const QMetaObject *meta = realObject->metaObject();
00481 QVariant v = toVariant(meta, p);
00482
00483 MetaDataBaseItem *item = 0;
00484 if (core()->metaDataBase())
00485 item = static_cast<MetaDataBaseItem*>(core()->metaDataBase()->item(realObject));
00486
00487 if (!item) {
00488 qWarning() << "** WARNING no ``meta database item'' for object:" << realObject;
00489 }
00490
00491 if (p->kind() == DomProperty::String && item) {
00492 DomString *str = p->elementString();
00493 if (str->hasAttributeComment()) {
00494 item->setPropertyComment(propertyName, str->attributeComment());
00495 }
00496 }
00497
00498
00499 if (QLayout *layout = qobject_cast<QLayout*>(o)) {
00500 if (propertyName == QLatin1String("margin") && qobject_cast<QLayoutWidget*>(layout->parentWidget()))
00501 v = v.toInt() + 1;
00502 }
00503
00504 sheet->setProperty(index, v);
00505 sheet->setChanged(index, true);
00506 }
00507
00508 if (propertyName == QLatin1String("objectName"))
00509 changeObjectName(o, o->objectName());
00510 }
00511 QSplitter *splitter = qobject_cast<QSplitter *>(o);
00512 if (splitter) {
00513 QDesignerWidgetFactoryInterface *widgetFactory = core()->widgetFactory();
00514 widgetFactory->createLayout(splitter, 0, splitter->orientation() == Qt::Horizontal ?
00515 LayoutInfo::HBox : LayoutInfo::VBox);
00516 }
00517 }
00518 }
00519
00520 QWidget *QDesignerResource::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &_name)
00521 {
00522 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(parentWidget))
00523 parentWidget = promoted->child();
00524
00525 QString name = _name;
00526 QString className = widgetName;
00527 if (m_isMainWidget)
00528 m_isMainWidget = false;
00529
00530 QWidget *w = core()->widgetFactory()->createWidget(className, parentWidget);
00531 if (!w)
00532 return 0;
00533
00534 if (name.isEmpty()) {
00535 QDesignerWidgetDataBaseInterface *db = core()->widgetDataBase();
00536 if (QDesignerWidgetDataBaseItemInterface *item = db->item(db->indexOfObject(w)))
00537 name = qtify(item->name());
00538 }
00539
00540 changeObjectName(w, name);
00541
00542 QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget);
00543 if (!qobject_cast<QMenu*>(w) && (!parentWidget || !container)) {
00544 m_formWindow->manageWidget(w);
00545 } else {
00546 core()->metaDataBase()->add(w);
00547 }
00548
00549 w->setWindowFlags(w->windowFlags() & ~Qt::Window);
00550
00551 return w;
00552 }
00553
00554 QLayout *QDesignerResource::createLayout(const QString &layoutName, QObject *parent, const QString &name)
00555 {
00556 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(parent))
00557 parent = promoted->child();
00558
00559 QWidget *layoutBase = 0;
00560 QLayout *layout = qobject_cast<QLayout*>(parent);
00561
00562 if (parent->isWidgetType())
00563 layoutBase = static_cast<QWidget*>(parent);
00564 else {
00565 Q_ASSERT( layout != 0 );
00566 layoutBase = layout->parentWidget();
00567 }
00568
00569 LayoutInfo::Type layoutType = LayoutInfo::Grid;
00570 if (layoutName == QLatin1String("QVBoxLayout"))
00571 layoutType = LayoutInfo::VBox;
00572 else if (layoutName == QLatin1String("QHBoxLayout"))
00573 layoutType = LayoutInfo::HBox;
00574 else if (layoutName == QLatin1String("QStackedLayout"))
00575 layoutType = LayoutInfo::Stacked;
00576
00577 QLayout *lay = core()->widgetFactory()->createLayout(layoutBase, layout, layoutType);
00578 if (lay != 0)
00579 changeObjectName(lay, name);
00580
00581 return lay;
00582 }
00583
00584
00585 DomWidget *QDesignerResource::createDom(QWidget *widget, DomWidget *ui_parentWidget, bool recursive)
00586 {
00587 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(widget);
00588 if (!item)
00589 return 0;
00590
00591 if (qobject_cast<Spacer*>(widget) && m_copyWidget == false) {
00592 ++m_topLevelSpacerCount;
00593 return 0;
00594 }
00595
00596 QDesignerWidgetDataBaseItemInterface *widgetInfo = 0;
00597 int widgetInfoIndex = core()->widgetDataBase()->indexOfObject(widget, false);
00598 if (widgetInfoIndex != -1) {
00599 widgetInfo = core()->widgetDataBase()->item(widgetInfoIndex);
00600
00601 if (widgetInfo->isCustom()) {
00602 if (widgetInfo->extends().isEmpty()) {
00603 const QMetaObject *mo = widget->metaObject()->superClass();
00604 while (mo != 0) {
00605 if (core()->widgetDataBase()->indexOfClassName(QLatin1String(mo->className())) != -1) {
00606 widgetInfo->setExtends(QLatin1String(mo->className()));
00607 break;
00608 }
00609 mo = mo->superClass();
00610 }
00611 }
00612 m_usedCustomWidgets.insert(widgetInfo, true);
00613 }
00614 }
00615
00616 DomWidget *w = 0;
00617
00618 if (QDesignerTabWidget *tabWidget = qobject_cast<QDesignerTabWidget*>(widget))
00619 w = saveWidget(tabWidget, ui_parentWidget);
00620 else if (QDesignerStackedWidget *stackedWidget = qobject_cast<QDesignerStackedWidget*>(widget))
00621 w = saveWidget(stackedWidget, ui_parentWidget);
00622 else if (QDesignerToolBox *toolBox = qobject_cast<QDesignerToolBox*>(widget))
00623 w = saveWidget(toolBox, ui_parentWidget);
00624 else if (QDesignerToolBar *toolBar = qobject_cast<QDesignerToolBar*>(widget))
00625 w = saveWidget(toolBar, ui_parentWidget);
00626 else if (QDesignerDockWidget *dockWidget = qobject_cast<QDesignerDockWidget*>(widget))
00627 w = saveWidget(dockWidget, ui_parentWidget);
00628 else if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget))
00629 w = saveWidget(widget, container, ui_parentWidget);
00630 else if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(widget))
00631 w = createDom(promoted->child(), ui_parentWidget, recursive);
00632 else
00633 w = QAbstractFormBuilder::createDom(widget, ui_parentWidget, recursive);
00634
00635 Q_ASSERT( w != 0 );
00636
00637 if (!qobject_cast<QLayoutWidget*>(widget) && w->attributeClass() == QLatin1String("QWidget")) {
00638 w->setAttributeNative(true);
00639 }
00640
00641 QString className = w->attributeClass();
00642 if (m_internal_to_qt.contains(className))
00643 w->setAttributeClass(m_internal_to_qt.value(className));
00644
00645 w->setAttributeName(widget->objectName());
00646
00647 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(widget)) {
00648 Q_ASSERT(widgetInfo != 0);
00649
00650 w->setAttributeName(promoted->child()->objectName());
00651 w->setAttributeClass(widgetInfo->name());
00652
00653 QList<DomProperty*> prop_list = w->elementProperty();
00654 foreach (DomProperty *prop, prop_list) {
00655 if (prop->attributeName() == QLatin1String("geometry")) {
00656 if (DomRect *rect = prop->elementRect()) {
00657 rect->setElementX(widget->x());
00658 rect->setElementY(widget->y());
00659 }
00660 break;
00661 }
00662 }
00663 } else if (widgetInfo != 0 && m_usedCustomWidgets.contains(widgetInfo)) {
00664 if (widgetInfo->name() != w->attributeClass())
00665 w->setAttributeClass(widgetInfo->name());
00666 }
00667
00668 if (QDesignerExtraInfoExtension *extra = qt_extension<QDesignerExtraInfoExtension*>(core()->extensionManager(), widget)) {
00669 extra->saveWidgetExtraInfo(w);
00670 }
00671
00672 return w;
00673 }
00674
00675 DomLayout *QDesignerResource::createDom(QLayout *layout, DomLayout *ui_parentLayout, DomWidget *ui_parentWidget)
00676 {
00677 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(layout);
00678
00679 if (item == 0) {
00680 layout = qFindChild<QLayout*>(layout);
00681
00682 item = core()->metaDataBase()->item(layout);
00683 }
00684
00685 if (item == 0) {
00686
00687 return 0;
00688 }
00689
00690 if (qobject_cast<QSplitter*>(layout->parentWidget()) != 0) {
00691
00692 return 0;
00693 }
00694
00695 m_chain.push(layout);
00696
00697 DomLayout *l = QAbstractFormBuilder::createDom(layout, ui_parentLayout, ui_parentWidget);
00698 Q_ASSERT(l != 0);
00699
00700 m_chain.pop();
00701
00702 return l;
00703 }
00704
00705 DomLayoutItem *QDesignerResource::createDom(QLayoutItem *item, DomLayout *ui_layout, DomWidget *ui_parentWidget)
00706 {
00707 DomLayoutItem *ui_item = 0;
00708
00709 if (Spacer *s = qobject_cast<Spacer*>(item->widget())) {
00710 if (!core()->metaDataBase()->item(s))
00711 return 0;
00712
00713 DomSpacer *spacer = new DomSpacer();
00714 QList<DomProperty*> properties = computeProperties(item->widget());
00715
00716 spacer->setElementProperty(properties);
00717
00718 ui_item = new DomLayoutItem();
00719 ui_item->setElementSpacer(spacer);
00720 m_laidout.insert(item->widget(), true);
00721 } else if (QLayoutWidget *layoutWidget = qobject_cast<QLayoutWidget*>(item->widget())) {
00722 Q_ASSERT(layoutWidget->layout());
00723 DomLayout *l = createDom(layoutWidget->layout(), ui_layout, ui_parentWidget);
00724 ui_item = new DomLayoutItem();
00725 ui_item->setElementLayout(l);
00726 m_laidout.insert(item->widget(), true);
00727 } else if (!item->spacerItem()) {
00728 ui_item = QAbstractFormBuilder::createDom(item, ui_layout, ui_parentWidget);
00729 } else {
00730 return 0;
00731 }
00732
00733 if (m_chain.size() && item->widget()) {
00734 if (QGridLayout *grid = qobject_cast<QGridLayout*>(m_chain.top())) {
00735 int index = Utils::indexOfWidget(grid, item->widget());
00736
00737 int row, column, rowspan, colspan;
00738 grid->getItemPosition(index, &row, &column, &rowspan, &colspan);
00739 ui_item->setAttributeRow(row);
00740 ui_item->setAttributeColumn(column);
00741
00742 if (colspan != 1)
00743 ui_item->setAttributeColSpan(colspan);
00744
00745 if (rowspan != 1)
00746 ui_item->setAttributeRowSpan(rowspan);
00747 }
00748 }
00749
00750 return ui_item;
00751 }
00752
00753 void QDesignerResource::createCustomWidgets(DomCustomWidgets *dom_custom_widgets)
00754 {
00755 if (dom_custom_widgets == 0)
00756 return;
00757 QList<DomCustomWidget*> custom_widget_list = dom_custom_widgets->elementCustomWidget();
00758 QDesignerWidgetDataBaseInterface *db = m_formWindow->core()->widgetDataBase();
00759 foreach(DomCustomWidget *custom_widget, custom_widget_list) {
00760 WidgetDataBaseItem *item
00761 = new WidgetDataBaseItem(custom_widget->elementClass());
00762 QString base_class = custom_widget->elementExtends();
00763 item->setExtends(base_class);
00764 item->setPromoted(!base_class.isEmpty());
00765 item->setGroup(base_class.isEmpty() ? QApplication::translate("Designer", "Custom Widgets")
00766 : QApplication::translate("Designer", "Promoted Widgets"));
00767 if (DomHeader *header = custom_widget->elementHeader())
00768 item->setIncludeFile(header->text());
00769 item->setContainer(custom_widget->elementContainer());
00770 item->setCustom(true);
00771 db->append(item);
00772 }
00773 }
00774
00775 DomTabStops *QDesignerResource::saveTabStops()
00776 {
00777 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);
00778 Q_ASSERT(item);
00779
00780 QStringList tabStops;
00781 foreach (QWidget *widget, item->tabOrder()) {
00782 if (m_formWindow->mainContainer()->isAncestorOf(widget))
00783 tabStops.append(widget->objectName());
00784 }
00785
00786 if (tabStops.count()) {
00787 DomTabStops *dom = new DomTabStops;
00788 dom->setElementTabStop(tabStops);
00789 return dom;
00790 }
00791
00792 return 0;
00793 }
00794
00795 void QDesignerResource::applyTabStops(QWidget *widget, DomTabStops *tabStops)
00796 {
00797 if (!tabStops)
00798 return;
00799
00800 QList<QWidget*> tabOrder;
00801 foreach (QString widgetName, tabStops->elementTabStop()) {
00802 if (QWidget *w = qFindChild<QWidget*>(widget, widgetName)) {
00803 tabOrder.append(w);
00804 }
00805 }
00806
00807 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);
00808 Q_ASSERT(item);
00809 item->setTabOrder(tabOrder);
00810 }
00811
00812 DomWidget *QDesignerResource::saveWidget(QWidget *widget, QDesignerContainerExtension *container, DomWidget *ui_parentWidget)
00813 {
00814 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
00815 QList<DomWidget*> ui_widget_list;
00816
00817 for (int i=0; i<container->count(); ++i) {
00818 QWidget *page = container->widget(i);
00819 Q_ASSERT(page);
00820
00821 DomWidget *ui_page = createDom(page, ui_widget);
00822 Q_ASSERT( ui_page != 0 );
00823
00824 ui_widget_list.append(ui_page);
00825 }
00826
00827 ui_widget->setElementWidget(ui_widget_list);
00828
00829 return ui_widget;
00830 }
00831
00832 DomWidget *QDesignerResource::saveWidget(QDesignerStackedWidget *widget, DomWidget *ui_parentWidget)
00833 {
00834 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
00835 QList<DomWidget*> ui_widget_list;
00836 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
00837 for (int i=0; i<container->count(); ++i) {
00838 QWidget *page = container->widget(i);
00839 Q_ASSERT(page);
00840
00841 DomWidget *ui_page = createDom(page, ui_widget);
00842 Q_ASSERT( ui_page != 0 );
00843
00844 ui_widget_list.append(ui_page);
00845 }
00846 }
00847
00848 ui_widget->setElementWidget(ui_widget_list);
00849
00850 return ui_widget;
00851 }
00852
00853 DomWidget *QDesignerResource::saveWidget(QDesignerToolBar *toolBar, DomWidget *ui_parentWidget)
00854 {
00855 DomWidget *ui_widget = QAbstractFormBuilder::createDom(toolBar, ui_parentWidget, false);
00856 if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(toolBar->parentWidget())) {
00857 Qt::ToolBarArea area = mainWindow->toolBarArea(toolBar);
00858 DomProperty *attr = new DomProperty();
00859 attr->setAttributeName(QLatin1String("toolBarArea"));
00860 attr->setElementNumber(int(area));
00861 ui_widget->setElementAttribute(ui_widget->elementAttribute() << attr);
00862 }
00863
00864 return ui_widget;
00865 }
00866
00867 DomWidget *QDesignerResource::saveWidget(QDesignerDockWidget *dockWidget, DomWidget *ui_parentWidget)
00868 {
00869 DomWidget *ui_widget = QAbstractFormBuilder::createDom(dockWidget, ui_parentWidget, true);
00870 if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(dockWidget->parentWidget())) {
00871 Qt::DockWidgetArea area = mainWindow->dockWidgetArea(dockWidget);
00872 DomProperty *attr = new DomProperty();
00873 attr->setAttributeName(QLatin1String("dockWidgetArea"));
00874 attr->setElementNumber(int(area));
00875 ui_widget->setElementAttribute(ui_widget->elementAttribute() << attr);
00876 }
00877
00878 return ui_widget;
00879 }
00880
00881 DomProperty *QDesignerResource::createIconProperty(const QVariant &v) const
00882 {
00883 DomProperty *dom_prop = new DomProperty();
00884
00885 DomResourcePixmap *r = new DomResourcePixmap;
00886 QString icon_path;
00887 QString qrc_path;
00888 if (v.type() == QVariant::Icon) {
00889 QIcon icon = qvariant_cast<QIcon>(v);
00890 icon_path = iconToFilePath(icon);
00891 qrc_path = iconToQrcPath(icon);
00892 } else {
00893 QPixmap pixmap = qvariant_cast<QPixmap>(v);
00894 icon_path = pixmapToFilePath(pixmap);
00895 qrc_path = pixmapToQrcPath(pixmap);
00896 }
00897
00898 if (qrc_path.isEmpty())
00899 icon_path = workingDirectory().relativeFilePath(icon_path);
00900 else
00901 qrc_path = workingDirectory().relativeFilePath(qrc_path);
00902
00903 r->setText(icon_path);
00904 if (!qrc_path.isEmpty())
00905 r->setAttributeResource(qrc_path);
00906
00907 if (v.type() == QVariant::Icon)
00908 dom_prop->setElementIconSet(r);
00909 else
00910 dom_prop->setElementPixmap(r);
00911
00912 return dom_prop;
00913 }
00914
00915 DomWidget *QDesignerResource::saveWidget(QDesignerTabWidget *widget, DomWidget *ui_parentWidget)
00916 {
00917 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
00918 QList<DomWidget*> ui_widget_list;
00919
00920 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
00921 for (int i=0; i<container->count(); ++i) {
00922 QWidget *page = container->widget(i);
00923 Q_ASSERT(page);
00924
00925 DomWidget *ui_page = createDom(page, ui_widget);
00926 Q_ASSERT( ui_page != 0 );
00927
00928 QList<DomProperty*> ui_attribute_list;
00929
00930 DomProperty *p = 0;
00931 DomString *str = 0;
00932
00933
00934 p = new DomProperty();
00935 p->setAttributeName(QLatin1String("title"));
00936 str = new DomString();
00937 str->setText(widget->tabText(i));
00938 p->setElementString(str);
00939 ui_attribute_list.append(p);
00940
00941
00942 if (!widget->tabIcon(i).isNull()) {
00943 p = createIconProperty(widget->tabIcon(i));
00944 p->setAttributeName(QLatin1String("icon"));
00945 ui_attribute_list.append(p);
00946 }
00947
00948
00949 if (!widget->tabToolTip(i).isEmpty()) {
00950 p = new DomProperty();
00951 p->setAttributeName(QLatin1String("toolTip"));
00952 str = new DomString();
00953 str->setText(widget->tabToolTip(i));
00954 p->setElementString(str);
00955 ui_attribute_list.append(p);
00956 }
00957
00958 ui_page->setElementAttribute(ui_attribute_list);
00959
00960 ui_widget_list.append(ui_page);
00961 }
00962 }
00963
00964 ui_widget->setElementWidget(ui_widget_list);
00965
00966 return ui_widget;
00967 }
00968
00969 DomWidget *QDesignerResource::saveWidget(QDesignerToolBox *widget, DomWidget *ui_parentWidget)
00970 {
00971 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
00972 QList<DomWidget*> ui_widget_list;
00973
00974 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
00975 for (int i=0; i<container->count(); ++i) {
00976 QWidget *page = container->widget(i);
00977 Q_ASSERT(page);
00978
00979 DomWidget *ui_page = createDom(page, ui_widget);
00980 Q_ASSERT( ui_page != 0 );
00981
00982
00983 DomProperty *p = new DomProperty();
00984 p->setAttributeName(QLatin1String("label"));
00985 DomString *str = new DomString();
00986 str->setText(widget->itemText(i));
00987 p->setElementString(str);
00988
00989 QList<DomProperty*> ui_attribute_list;
00990 ui_attribute_list.append(p);
00991
00992
00993 if (!widget->itemIcon(i).isNull()) {
00994 p = createIconProperty(widget->itemIcon(i));
00995 p->setAttributeName(QLatin1String("icon"));
00996 ui_attribute_list.append(p);
00997 }
00998
00999
01000 if (!widget->itemToolTip(i).isEmpty()) {
01001 p = new DomProperty();
01002 p->setAttributeName(QLatin1String("toolTip"));
01003 str = new DomString();
01004 str->setText(widget->itemToolTip(i));
01005 p->setElementString(str);
01006 ui_attribute_list.append(p);
01007 }
01008
01009 ui_page->setElementAttribute(ui_attribute_list);
01010
01011 ui_widget_list.append(ui_page);
01012 }
01013 }
01014
01015 ui_widget->setElementWidget(ui_widget_list);
01016
01017 return ui_widget;
01018 }
01019
01020 bool QDesignerResource::checkProperty(QObject *obj, const QString &prop) const
01021 {
01022 const QMetaObject *meta = obj->metaObject();
01023 int pindex = meta->indexOfProperty(prop.toLatin1());
01024 if (pindex != -1) {
01025 if (!meta->property(pindex).isStored(obj))
01026 return false;
01027 }
01028
01029 if (prop == QLatin1String("objectName")) {
01030 return false;
01031 } else if (prop == QLatin1String("geometry") && obj->isWidgetType()) {
01032 QWidget *check_widget = qobject_cast<QWidget*>(obj);
01033 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(obj->parent()))
01034 check_widget = promoted;
01035
01036 if (m_selected && m_selected == check_widget)
01037 return true;
01038
01039 return !LayoutInfo::isWidgetLaidout(core(), check_widget);
01040 }
01041
01042 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), obj)) {
01043 int pindex = sheet->indexOf(prop);
01044 if (sheet->isAttribute(pindex))
01045 return false;
01046
01047 return sheet->isChanged(pindex);
01048 }
01049
01050 return false;
01051 }
01052
01053 bool QDesignerResource::addItem(DomLayoutItem *ui_item, QLayoutItem *item, QLayout *layout)
01054 {
01055 if (item->widget() == 0) {
01056 return false;
01057 }
01058
01059 QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
01060 QBoxLayout *box = qobject_cast<QBoxLayout*>(layout);
01061
01062 if (grid != 0) {
01063 int rowSpan = ui_item->hasAttributeRowSpan() ? ui_item->attributeRowSpan() : 1;
01064 int colSpan = ui_item->hasAttributeColSpan() ? ui_item->attributeColSpan() : 1;
01065 add_to_grid_layout(grid, item->widget(), ui_item->attributeRow(), ui_item->attributeColumn(),
01066 rowSpan, colSpan, item->alignment());
01067 return true;
01068 } else if (box != 0) {
01069 add_to_box_layout(box, item->widget());
01070 return true;
01071 }
01072
01073 return QAbstractFormBuilder::addItem(ui_item, item, layout);
01074 }
01075
01076 bool QDesignerResource::addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
01077 {
01078 core()->metaDataBase()->add(widget);
01079
01080 if (! QAbstractFormBuilder::addItem(ui_widget, widget, parentWidget) || qobject_cast<QMainWindow*> (parentWidget)) {
01081 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget))
01082 container->addWidget(widget);
01083 }
01084
01085 return true;
01086 }
01087
01088 void QDesignerResource::copy(QIODevice *dev, const QList<QWidget*> &selection)
01089 {
01090 m_copyWidget = true;
01091
01092 DomUI *ui = copy(selection);
01093 QDomDocument doc;
01094 doc.appendChild(ui->write(doc));
01095 dev->write(doc.toString().toUtf8());
01096
01097 m_laidout.clear();
01098
01099 delete ui;
01100 m_copyWidget = false;
01101 }
01102
01103 DomUI *QDesignerResource::copy(const QList<QWidget*> &selection)
01104 {
01105 m_copyWidget = true;
01106
01107 DomUI *ui = new DomUI();
01108 ui->setAttributeVersion(QLatin1String("4.0"));
01109
01110 DomWidget *ui_widget = new DomWidget();
01111 QList<DomWidget*> ui_widget_list;
01112 ui_widget->setAttributeName(QLatin1String("__qt_fake_top_level"));
01113
01114 for (int i=0; i<selection.size(); ++i) {
01115 QWidget *w = selection.at(i);
01116 m_selected = w;
01117 DomWidget *ui_child = createDom(w, ui_widget);
01118 m_selected = 0;
01119 if (!ui_child)
01120 continue;
01121
01122 ui_widget_list.append(ui_child);
01123 }
01124
01125 ui_widget->setElementWidget(ui_widget_list);
01126 ui->setElementWidget(ui_widget);
01127
01128 m_laidout.clear();
01129
01130 m_copyWidget = false;
01131
01132 return ui;
01133 }
01134
01135 QList<QWidget*> QDesignerResource::paste(DomUI *ui, QWidget *parentWidget)
01136 {
01137 int saved = m_isMainWidget;
01138 m_isMainWidget = false;
01139 QList<QWidget*> createdWidgets;
01140
01141 DomWidget *topLevel = ui->elementWidget();
01142 QList<DomWidget*> widgets = topLevel->elementWidget();
01143 for (int i=0; i<widgets.size(); ++i) {
01144 QWidget *w = create(widgets.at(i), parentWidget);
01145 if (!w)
01146 continue;
01147
01148 w->move(w->pos() + m_formWindow->grid());
01149
01150 createdWidgets.append(w);
01151 }
01152
01153 m_isMainWidget = saved;
01154
01155 return createdWidgets;
01156 }
01157
01158 QList<QWidget*> QDesignerResource::paste(QIODevice *dev, QWidget *parentWidget)
01159 {
01160 QDomDocument doc;
01161 if (!doc.setContent(dev))
01162 return QList<QWidget*>();
01163
01164 QDomElement root = doc.firstChild().toElement();
01165 DomUI ui;
01166 ui.read(root);
01167 return paste(&ui, parentWidget);
01168 }
01169
01170 void QDesignerResource::layoutInfo(DomLayout *layout, QObject *parent, int *margin, int *spacing)
01171 {
01172 QAbstractFormBuilder::layoutInfo(layout, parent, margin, spacing);
01173
01174 QLayoutWidget *layoutWidget = qobject_cast<QLayoutWidget*>(parent);
01175 if (layoutWidget && margin) {
01176 if (*margin == INT_MIN)
01177 *margin = 1;
01178 else
01179 *margin = *margin + 1;
01180 }
01181 }
01182
01183 QString QDesignerResource::qtify(const QString &name)
01184 {
01185 QString qname = name;
01186
01187 if (qname.count() > 1 && qname.at(1).toUpper() == qname.at(1) && (qname.at(0) == QLatin1Char('Q') || qname.at(0) == QLatin1Char('K')))
01188 qname = qname.mid(1);
01189
01190 int i=0;
01191 while (i < qname.length()) {
01192 if (qname.at(i).toLower() != qname.at(i))
01193 qname[i] = qname.at(i).toLower();
01194 else
01195 break;
01196
01197 ++i;
01198 }
01199
01200 return qname;
01201 }
01202
01203 DomCustomWidgets *QDesignerResource::saveCustomWidgets()
01204 {
01205 if (m_usedCustomWidgets.isEmpty())
01206 return 0;
01207
01208 QList<DomCustomWidget*> custom_widget_list;
01209 foreach (QDesignerWidgetDataBaseItemInterface *item, m_usedCustomWidgets.keys()) {
01210 DomCustomWidget *custom_widget = new DomCustomWidget;
01211 custom_widget->setElementClass(item->name());
01212 if (item->isContainer())
01213 custom_widget->setElementContainer(item->isContainer());
01214
01215 if (!item->includeFile().isEmpty()) {
01216 DomHeader *header = new DomHeader;
01217 header->setText(item->includeFile());
01218 custom_widget->setElementHeader(header);
01219 custom_widget->setElementExtends(item->extends());
01220 }
01221
01222 custom_widget_list.append(custom_widget);
01223 }
01224
01225 DomCustomWidgets *customWidgets = new DomCustomWidgets;
01226 customWidgets->setElementCustomWidget(custom_widget_list);
01227 return customWidgets;
01228 }
01229
01230 QList<DomProperty*> QDesignerResource::computeProperties(QObject *object)
01231 {
01232 QList<DomProperty*> properties;
01233 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), object)) {
01234 for (int index = 0; index < sheet->count(); ++index) {
01235 QString propertyName = sheet->propertyName(index);
01236 QVariant value = sheet->property(index);
01237
01238 if (QLayout *layout = qobject_cast<QLayout*>(object)) {
01239 if (propertyName == QLatin1String("margin") && qobject_cast<QLayoutWidget*>(layout->parentWidget()))
01240 value = value.toInt() - 1;
01241 }
01242
01243 if (!sheet->isChanged(index))
01244 continue;
01245
01246 if (DomProperty *p = createProperty(object, propertyName, value)) {
01247 if (p->kind() == DomProperty::String && qobject_cast<MetaDataBase*>(core()->metaDataBase())) {
01248 MetaDataBaseItem *item = static_cast<MetaDataBaseItem*>(core()->metaDataBase()->item(object));
01249
01250 if (item && !item->propertyComment(propertyName).isEmpty()) {
01251 p->elementString()->setAttributeComment(item->propertyComment(propertyName));
01252 }
01253 }
01254
01255 properties.append(p);
01256 }
01257 }
01258 }
01259 return properties;
01260 }
01261
01262 DomProperty *QDesignerResource::createProperty(QObject *object, const QString &propertyName, const QVariant &value)
01263 {
01264 if (!checkProperty(object, propertyName)) {
01265 return 0;
01266 }
01267
01268 QExtensionManager *mgr = core()->extensionManager();
01269 QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*> (mgr, core());
01270
01271 if (qVariantCanConvert<EnumType>(value)) {
01272 EnumType e = qvariant_cast<EnumType>(value);
01273 int v = e.value.toInt();
01274 QMapIterator<QString, QVariant> it(e.items);
01275 while (it.hasNext()) {
01276 if (it.next().value().toInt() != v)
01277 continue;
01278
01279 DomProperty *p = new DomProperty;
01280
01281 const QMetaObject *meta = object->metaObject();
01282 int pindex = meta->indexOfProperty(propertyName.toLatin1());
01283 if (pindex != -1) {
01284 QMetaProperty meta_property = meta->property(pindex);
01285 if (!meta_property.hasStdCppSet())
01286 p->setAttributeStdset(0);
01287 }
01288 p->setAttributeName(propertyName);
01289
01290 QString id = it.key();
01291 if (lang)
01292 id = lang->neutralEnumerator(id);
01293
01294 p->setElementEnum(id);
01295 return p;
01296 }
01297
01298 return 0;
01299 } else if (qVariantCanConvert<FlagType>(value)) {
01300 FlagType f = qvariant_cast<FlagType>(value);
01301 uint v = f.value.toUInt();
01302 QMapIterator<QString, QVariant> it(f.items);
01303 QStringList keys;
01304
01305 while (it.hasNext()) {
01306 uint x = it.next().value().toUInt();
01307
01308 QString id = it.key();
01309 if (lang)
01310 id = lang->neutralEnumerator(id);
01311
01312 if (v == x) {
01313 DomProperty *p = new DomProperty;
01314
01315 const QMetaObject *meta = object->metaObject();
01316 int pindex = meta->indexOfProperty(propertyName.toLatin1());
01317 if (pindex != -1) {
01318 QMetaProperty meta_property = meta->property(pindex);
01319 if (!meta_property.hasStdCppSet())
01320 p->setAttributeStdset(0);
01321 }
01322 p->setAttributeName(propertyName);
01323
01324 p->setElementSet(id);
01325 return p;
01326 }
01327
01328 if ((v & x) == x)
01329 keys.push_back(id);
01330 }
01331
01332 if (keys.isEmpty())
01333 return 0;
01334
01335 DomProperty *p = new DomProperty;
01336 p->setAttributeName(propertyName);
01337 p->setElementSet(keys.join(QLatin1String("|")));
01338 return p;
01339 }
01340
01341 return QAbstractFormBuilder::createProperty(object, propertyName, value);
01342 }
01343
01344 void QDesignerResource::createResources(DomResources *resources)
01345 {
01346 if (resources == 0)
01347 return;
01348
01349 QList<DomResource*> dom_include = resources->elementInclude();
01350 foreach (DomResource *res, dom_include) {
01351 QString path = m_formWindow->absoluteDir().absoluteFilePath(res->attributeLocation());
01352 while (!QFile::exists(path)) {
01353 if (QMessageBox::warning(m_formWindow->core()->topLevel(), QApplication::translate("qdesigner_internal::QDesignerResource",
01354 "Loading qrc file", 0, QApplication::UnicodeUTF8),
01355 QApplication::translate("qdesigner_internal::QDesignerResource",
01356 "The specified qrc file <p><b>%1</b></p><p>could not be found. Do you want to update the file location?</p>", 0, QApplication::UnicodeUTF8).arg(path),
01357 QApplication::translate("qdesigner_internal::QDesignerResource", "&Yes", 0, QApplication::UnicodeUTF8),
01358 QApplication::translate("qdesigner_internal::QDesignerResource", "&No", 0, QApplication::UnicodeUTF8),
01359 QString(), 0, 1) == 0) {
01360 QFileInfo fi(path);
01361 path = QFileDialog::getOpenFileName(m_formWindow->core()->topLevel(),
01362 QApplication::translate("qdesigner_internal::QDesignerResource",
01363 "New location for %1", 0, QApplication::UnicodeUTF8).arg(fi.fileName()), fi.absolutePath(),
01364 QApplication::translate("qdesigner_internal::QDesignerResource", "Resource files (*.qrc)", 0, QApplication::UnicodeUTF8));
01365 if (path.isEmpty())
01366 break;
01367 } else {
01368 break;
01369 }
01370 }
01371 if (!path.isEmpty())
01372 m_formWindow->addResourceFile(path);
01373 }
01374 }
01375
01376 DomResources *QDesignerResource::saveResources()
01377 {
01378 QStringList res_list = m_formWindow->resourceFiles();
01379 QList<DomResource*> dom_include;
01380 foreach (QString res, res_list) {
01381 DomResource *dom_res = new DomResource;
01382 QString conv_path = m_formWindow->absoluteDir().relativeFilePath(res);
01383 dom_res->setAttributeLocation(conv_path.replace(QDir::separator(), QLatin1Char('/')));
01384 dom_include.append(dom_res);
01385 }
01386
01387 DomResources *dom_resources = new DomResources;
01388 dom_resources->setElementInclude(dom_include);
01389
01390 return dom_resources;
01391 }
01392
01393 DomAction *QDesignerResource::createDom(QAction *action)
01394 {
01395 if (!core()->metaDataBase()->item(action) || action->menu())
01396 return 0;
01397
01398 return QAbstractFormBuilder::createDom(action);
01399 }
01400
01401 DomActionGroup *QDesignerResource::createDom(QActionGroup *actionGroup)
01402 {
01403 if (core()->metaDataBase()->item(actionGroup) != 0) {
01404 return QAbstractFormBuilder::createDom(actionGroup);
01405 }
01406
01407 return 0;
01408 }
01409
01410 QAction *QDesignerResource::create(DomAction *ui_action, QObject *parent)
01411 {
01412 if (QAction *action = QAbstractFormBuilder::create(ui_action, parent)) {
01413 core()->metaDataBase()->add(action);
01414 return action;
01415 }
01416
01417 return 0;
01418 }
01419
01420 QActionGroup *QDesignerResource::create(DomActionGroup *ui_action_group, QObject *parent)
01421 {
01422 if (QActionGroup *actionGroup = QAbstractFormBuilder::create(ui_action_group, parent)) {
01423 core()->metaDataBase()->add(actionGroup);
01424 return actionGroup;
01425 }
01426
01427 return 0;
01428 }
01429
01430 DomActionRef *QDesignerResource::createActionRefDom(QAction *action)
01431 {
01432 if (!core()->metaDataBase()->item(action)
01433 || qobject_cast<SentinelAction*>(action)
01434 || (!action->isSeparator() && !action->menu() && action->objectName().isEmpty()))
01435 return 0;
01436
01437 return QAbstractFormBuilder::createActionRefDom(action);
01438 }
01439
01440 void QDesignerResource::addMenuAction(QAction *action)
01441 {
01442 core()->metaDataBase()->add(action);
01443 }
01444
01445 QAction *QDesignerResource::createAction(QObject *parent, const QString &name)
01446 {
01447 if (QAction *action = QAbstractFormBuilder::createAction(parent, name)) {
01448 core()->metaDataBase()->add(action);
01449 return action;
01450 }
01451
01452 return 0;
01453 }
01454
01455 QActionGroup *QDesignerResource::createActionGroup(QObject *parent, const QString &name)
01456 {
01457 if (QActionGroup *actionGroup = QAbstractFormBuilder::createActionGroup(parent, name)) {
01458 core()->metaDataBase()->add(actionGroup);
01459 return actionGroup;
01460 }
01461
01462 return 0;
01463 }
01464
01465 void QDesignerResource::loadExtraInfo(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
01466 {
01467 if (QDesignerPromotedWidget *promoted = qobject_cast<QDesignerPromotedWidget*>(widget))
01468 widget = promoted->child();
01469 QAbstractFormBuilder::loadExtraInfo(ui_widget, widget, parentWidget);
01470 }