tools/designer/src/designer/qdesigner_formwindow.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the Qt Designer of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qdesigner_actions.h"
00025 #include "qdesigner_formwindow.h"
00026 #include "qdesigner_workbench.h"
00027 #include "qdesigner_settings.h"
00028 
00029 // sdk
00030 #include <QtDesigner/QtDesigner>
00031 
00032 // shared
00033 #include <QtGui/QUndoCommand>
00034 #include <qdesigner_command_p.h>
00035 
00036 #include <QtCore/QEvent>
00037 #include <QtCore/QFile>
00038 #include <QtCore/QTimer>
00039 #include <QtCore/qdebug.h>
00040 
00041 #include <QtGui/QAction>
00042 #include <QtGui/QCloseEvent>
00043 #include <QtGui/QFileDialog>
00044 #include <QtGui/QMessageBox>
00045 
00046 QDesignerFormWindow::QDesignerFormWindow(QDesignerFormWindowInterface *editor, QDesignerWorkbench *workbench, QWidget *parent, Qt::WindowFlags flags)
00047     : QMainWindow(parent, flags),
00048       m_editor(editor),
00049       m_workbench(workbench),
00050       initialized(false)
00051 {
00052     Q_ASSERT(workbench);
00053 
00054     setMaximumSize(0xFFF, 0xFFF);
00055 
00056     if (m_editor) {
00057         m_editor->setParent(this);
00058     } else {
00059         m_editor = workbench->core()->formWindowManager()->createFormWindow(this);
00060     }
00061 
00062     setCentralWidget(m_editor);
00063 
00064     m_action = new QAction(this);
00065     m_action->setCheckable(true);
00066 
00067     connect(m_editor->commandHistory(), SIGNAL(indexChanged(int)), this, SLOT(updateChanged()));
00068     connect(m_editor, SIGNAL(fileNameChanged(QString)), this, SLOT(updateWindowTitle(QString)));
00069     connect(m_editor, SIGNAL(geometryChanged()), this, SLOT(geometryChanged()));
00070 }
00071 
00072 QDesignerFormWindow::~QDesignerFormWindow()
00073 {
00074     if (workbench())
00075         workbench()->removeFormWindow(this);
00076 }
00077 
00078 QAction *QDesignerFormWindow::action() const
00079 {
00080     return m_action;
00081 }
00082 
00083 void QDesignerFormWindow::changeEvent(QEvent *e)
00084 {
00085     switch (e->type()) {
00086         case QEvent::ActivationChange: {
00087             if (isActiveWindow()) {
00088                 m_action->setChecked(true);
00089                 // ### raise();
00090             }
00091         } break;
00092         case QEvent::WindowTitleChange:
00093             m_action->setText(windowTitle().replace(QLatin1String("[*]"), ""));
00094             break;
00095         case QEvent::WindowIconChange:
00096             m_action->setIcon(windowIcon());
00097             break;
00098         default:
00099             break;
00100     }
00101     QMainWindow::changeEvent(e);
00102 }
00103 
00104 QRect QDesignerFormWindow::geometryHint() const
00105 {
00106     return QRect(0, 0, 400, 300);
00107 }
00108 
00109 QDesignerFormWindowInterface *QDesignerFormWindow::editor() const
00110 {
00111     return m_editor;
00112 }
00113 
00114 QDesignerWorkbench *QDesignerFormWindow::workbench() const
00115 {
00116     return m_workbench;
00117 }
00118 
00119 void QDesignerFormWindow::updateWindowTitle(const QString &fileName)
00120 {    
00121     QString fn = QFileInfo(fileName).fileName();
00122 
00123     if (fn.isEmpty()) {
00124         // Try to preserve its "untitled" number.
00125         QRegExp rx(QLatin1String("unnamed( (\\d+))?"));
00126 
00127         if (rx.indexIn(windowTitle()) != -1) {
00128             fn = rx.cap(0);
00129         } else {
00130             fn = QLatin1String("untitled");
00131         }
00132     }
00133 
00134     if (QWidget *mc = m_editor->mainContainer()) {
00135         setWindowIcon(mc->windowIcon());
00136         setWindowTitle(tr("%1 - %2[*]").arg(mc->windowTitle()).arg(fn));
00137     } else {
00138         setWindowTitle(fn);
00139     }
00140 }
00141 
00142 void QDesignerFormWindow::closeEvent(QCloseEvent *ev)
00143 {
00144     if (m_editor->isDirty()) {
00145         raise();
00146         QMessageBox box(tr("Save Form?"),
00147                 tr("Do you want to save the changes you made to \"%1\" before closing?")
00148                 .arg(m_editor->fileName().isEmpty() ? action()->text() : m_editor->fileName()),
00149                 QMessageBox::Information,
00150                 QMessageBox::Yes | QMessageBox::Default, QMessageBox::No,
00151                 QMessageBox::Cancel | QMessageBox::Escape, m_editor, Qt::Sheet);
00152         box.setButtonText(QMessageBox::Yes, m_editor->fileName().isEmpty() ? tr("Save...") : tr("Save"));
00153         box.setButtonText(QMessageBox::No, tr("Don't Save"));
00154         switch (box.exec()) {
00155             case QMessageBox::Yes: {
00156                 bool ok = workbench()->saveForm(m_editor);
00157                 ev->setAccepted(ok);
00158                 m_editor->setDirty(!ok);
00159                 break;
00160             }
00161             case QMessageBox::No:
00162                 m_editor->setDirty(false); // Not really necessary, but stops problems if we get close again.
00163                 ev->accept();
00164                 break;
00165             case QMessageBox::Cancel:
00166                 ev->ignore();
00167                 break;
00168         }
00169     }
00170 
00171     if (m_workbench->core()->formWindowManager()->formWindowCount() == 1 && ev->isAccepted()
00172             && QDesignerSettings().showNewFormOnStartup())
00173         QTimer::singleShot(200, m_workbench->actionManager(), SLOT(createForm()));  // Use timer in case we are quitting.
00174 }
00175 
00176 void QDesignerFormWindow::updateChanged()
00177 {
00178     setWindowModified(m_editor->isDirty());
00179     updateWindowTitle(m_editor->fileName());
00180 }
00181 
00182 void QDesignerFormWindow::resizeEvent(QResizeEvent *rev)
00183 {
00184     if(initialized) {
00185         m_editor->setDirty(true);
00186         setWindowModified(true);
00187     }
00188 
00189     initialized = true;
00190     QMainWindow::resizeEvent(rev);
00191 
00192     // update the maincontainer on resize
00193     m_editor->mainContainer()->raise();
00194     m_editor->mainContainer()->update();
00195 }
00196 
00197 void QDesignerFormWindow::geometryChanged()
00198 {
00199     if(QObject *object = m_editor->core()->propertyEditor()->object()) {
00200         QDesignerPropertySheetExtension *sheet = 
00201             qt_extension<QDesignerPropertySheetExtension*>(m_editor->core()->extensionManager(), object);
00202         m_editor->core()->propertyEditor()->setPropertyValue("geometry", sheet->property(sheet->indexOf("geometry")));
00203     }
00204 }

Generated on Thu Mar 15 12:01:20 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1