qdesigner_workbench.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,252 行 · 第 1/3 页

CPP
1,252
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qdesigner_workbench.h"#include "qdesigner.h"#include "preferences.h"#include "qdesigner_actions.h"#include "qdesigner_toolwindow.h"#include "qdesigner_formwindow.h"#include "qdesigner_settings.h"#include "qdesigner_widgetbox.h"#include "qdesigner_propertyeditor.h"#include "qdesigner_objectinspector.h"#include "qdesigner_signalsloteditor.h"#include "qdesigner_actioneditor.h"#include "qdesigner_resourceeditor.h"#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QDesignerFormWindowInterface>#include <QtDesigner/QDesignerFormWindowManagerInterface>#include <QtDesigner/QDesignerFormEditorPluginInterface>#include <QtDesigner/QDesignerWidgetBoxInterface>#include <QtDesigner/QDesignerMetaDataBaseInterface>#include <QtDesigner/QDesignerComponents>#include <QtDesigner/private/qdesigner_integration_p.h>#include <QtDesigner/private/pluginmanager_p.h>#include <QtDesigner/private/formwindowbase_p.h>#include <QtCore/QDir>#include <QtCore/QFile>#include <QtCore/QUrl>#include <QtCore/QTimer>#include <QtCore/QPluginLoader>#include <QtCore/qdebug.h>#include <QtGui/QActionGroup>#include <QtGui/QCloseEvent>#include <QtGui/QDesktopWidget>#include <QtGui/QDockWidget>#include <QtGui/QMenu>#include <QtGui/QMenuBar>#include <QtGui/QMessageBox>#include <QtGui/QPushButton>#include <QtGui/QToolBar>#include <QtGui/QMdiArea>#include <QtGui/QMdiSubWindow>static QMdiSubWindow *mdiSubWindowOf(const QWidget *w){    QMdiSubWindow *rc = qobject_cast<QMdiSubWindow *>(w->parentWidget());    Q_ASSERT(rc);    return rc;}static QDockWidget *dockWidgetOf(const QWidget *w){    for (QWidget *parentWidget = w->parentWidget(); parentWidget ; parentWidget = parentWidget->parentWidget()) {        if (QDockWidget *dw = qobject_cast<QDockWidget *>(parentWidget)) {            return dw;        }    }    Q_ASSERT("Dock widget not found");    return 0;}QMdiSubWindow *createFormMdiSubWindow(QMdiArea *a, QDesignerFormWindow *fw, Qt::WindowFlags f, const QKeySequence &designerCloseActionShortCut){    typedef QList<QAction *> ActionList;    QMdiSubWindow *rc = a->addSubWindow(fw, f);    // Make action shortcuts respond only if focused to avoid conflicts with designer menu actions    if (designerCloseActionShortCut == QKeySequence(QKeySequence::Close)) {        const ActionList systemMenuActions = rc->systemMenu()->actions();        if (!systemMenuActions.empty()) {            const ActionList::const_iterator cend = systemMenuActions.constEnd();            for (ActionList::const_iterator it = systemMenuActions.constBegin(); it != cend; ++it) {                if ( (*it)->shortcut() == designerCloseActionShortCut) {                    (*it)->setShortcutContext(Qt::WidgetShortcut);                    break;                }            }        }    }    rc->setMinimumSize(QSize(0, 0));    return rc;}// ------------ QDesignerWorkbench::PositionQDesignerWorkbench::Position::Position(const QMdiSubWindow *mdiSubWindow, const QPoint &mdiAreaOffset) :    m_minimized(mdiSubWindow->isShaded()),    m_position(mdiSubWindow->pos() + mdiAreaOffset){}QDesignerWorkbench::Position::Position(const QDockWidget *dockWidget) :    m_minimized(dockWidget->isMinimized()),    m_position(dockWidget->pos()){}QDesignerWorkbench::Position::Position(const QWidget *topLevelWindow, const QPoint &desktopTopLeft){    const QWidget *window =topLevelWindow->window ();    Q_ASSERT(window);    m_minimized = window->isMinimized();    m_position = window->pos() - desktopTopLeft;}void QDesignerWorkbench::Position::applyTo(QMdiSubWindow *mdiSubWindow,                                           const QPoint &mdiAreaOffset) const{    // QMdiSubWindow attempts to resize its children to sizeHint() when switching user interface modes.    // Restore old size    const QPoint mdiAreaPos =  QPoint(qMax(0, m_position.x() - mdiAreaOffset.x()),                                      qMax(0, m_position.y() - mdiAreaOffset.y()));    mdiSubWindow->move(mdiAreaPos);    const QSize decorationSize = mdiSubWindow->size() - mdiSubWindow->contentsRect().size();    mdiSubWindow->resize(mdiSubWindow->widget()->size() + decorationSize);    mdiSubWindow->show();    if (m_minimized) {        mdiSubWindow->showShaded();    }}void QDesignerWorkbench::Position::applyTo(QWidget *topLevelWindow, const QPoint &desktopTopLeft) const{    QWidget *window = topLevelWindow->window ();    const QPoint newPos = m_position + desktopTopLeft;    window->move(newPos);    if ( m_minimized) {        topLevelWindow->showMinimized();    } else {        topLevelWindow->show();    }}void QDesignerWorkbench::Position::applyTo(QDockWidget *dockWidget) const{    dockWidget->widget()->setVisible(true);    dockWidget->setVisible(!m_minimized);}// -------- QDesignerWorkbenchQDesignerWorkbench::QDesignerWorkbench()    : m_mode(NeutralMode), m_mdiArea(0), m_state(StateInitializing){    initialize();    applyPreferences(QDesignerSettings().preferences());    m_state = StateUp;}QDesignerWorkbench::~QDesignerWorkbench(){    QDesignerSettings settings;    settings.clearBackup();    if (m_mode == DockedMode) {        Q_ASSERT(m_mdiArea != 0);        QMainWindow *mw = qobject_cast<QMainWindow*>(m_mdiArea->window());        Q_ASSERT(mw != 0);        settings.setMainWindowState(mw->saveState(2));    } else if (m_mode == TopLevelMode) {        QDesignerToolWindow *widgetBoxWrapper = findToolWindow(core()->widgetBox());        if (widgetBoxWrapper) {            settings.setToolBoxState(widgetBoxWrapper->saveState());        }    }    while (!m_toolWindows.isEmpty())        delete m_toolWindows.takeLast();}void QDesignerWorkbench::saveGeometries(){    m_Positions.clear();    switch (m_mode) {    case NeutralMode:        break;    case TopLevelMode: {        const QPoint desktopOffset = QApplication::desktop()->availableGeometry().topLeft();        foreach (QDesignerToolWindow *tw, m_toolWindows)            m_Positions.insert(tw, Position(tw, desktopOffset));        foreach (QDesignerFormWindow *fw, m_formWindows) {            m_Positions.insert(fw,  Position(fw, desktopOffset));        }    }        break;    case DockedMode: {        const QPoint mdiAreaOffset = m_mdiArea->pos();        foreach (QDesignerToolWindow *tw, m_toolWindows) {            m_Positions.insert(tw, Position(dockWidgetOf(tw)));        }        foreach (QDesignerFormWindow *fw, m_formWindows) {            m_Positions.insert(fw, Position(mdiSubWindowOf(fw), mdiAreaOffset));        }    }        break;    }}UIMode QDesignerWorkbench::mode() const{    return m_mode;}void QDesignerWorkbench::addToolWindow(QDesignerToolWindow *toolWindow){    Q_ASSERT(m_toolWindowExtras.contains(toolWindow) == false);    Q_ASSERT(toolWindow->windowTitle().isEmpty() == false);    m_toolWindows.append(toolWindow);    if (QAction *action = toolWindow->action()) {        Q_ASSERT(m_toolMenu->actions().isEmpty() == false);        QList<QAction*> lst = m_toolActions->actions();        QAction *before = lst.count() ? lst.last() : m_toolMenu->actions().first();        m_toolMenu->insertAction(before, action);        m_toolActions->addAction(action);    }}void QDesignerWorkbench::addFormWindow(QDesignerFormWindow *formWindow){    Q_ASSERT(m_formWindowExtras.contains(formWindow) == false);    // ### Q_ASSERT(formWindow->windowTitle().isEmpty() == false);    m_formWindows.append(formWindow);    m_actionManager->setWindowListSeparatorVisible(true);    if (QAction *action = formWindow->action()) {        m_windowActions->addAction(action);        m_windowMenu->addAction(action);        action->setChecked(true);    }    m_actionManager->minimizeAction()->setEnabled(true);    m_actionManager->minimizeAction()->setChecked(false);    connect(formWindow, SIGNAL(minimizationStateChanged(QDesignerFormWindowInterface *, bool)),            this, SLOT(minimizationStateChanged(QDesignerFormWindowInterface *, bool)));    m_actionManager->editWidgets()->trigger();}void QDesignerWorkbench::initialize(){    QDesignerSettings settings;    m_core = QDesignerComponents::createFormEditor(this);    (void) QDesignerComponents::createTaskMenu(core(), this);    initializeCorePlugins();    QDesignerComponents::initializePlugins(core());    m_toolActions = new QActionGroup(this);    m_toolActions->setExclusive(false);    m_windowActions = new QActionGroup(this);    m_windowActions->setExclusive(true);    connect(m_windowActions, SIGNAL(triggered(QAction*)), this, SLOT(formWindowActionTriggered(QAction*)));    m_actionManager = new QDesignerActions(this);    m_globalMenuBar = new QMenuBar;    m_fileMenu = m_globalMenuBar->addMenu(tr("&File"));    foreach (QAction *action, m_actionManager->fileActions()->actions()) {        m_fileMenu->addAction(action);        if (action->text() == QDesignerActions::tr("&Open Form...")) {            QMenu *recentFilesMenu = m_fileMenu->addMenu(tr("&Recent Forms"));            // Pop the "Recent Files" stuff in here.            foreach(QAction *recentAction, m_actionManager->recentFilesActions()->actions())                recentFilesMenu->addAction(recentAction);        }    }    m_editMenu = m_globalMenuBar->addMenu(tr("&Edit"));    foreach (QAction *action, m_actionManager->editActions()->actions()) {        m_editMenu->addAction(action);    }    m_editMenu->addSeparator();    foreach (QAction *action, m_actionManager->toolActions()->actions()) {        m_editMenu->addAction(action);    }    m_editMenu->addSeparator();    m_editMenu->addAction(m_actionManager->preferencesAction());    m_formMenu = m_globalMenuBar->addMenu(tr("F&orm"));    foreach (QAction *action, m_actionManager->formActions()->actions()) {        m_formMenu->addAction(action);    }    QMenu *previewSubMenu = new QMenu(tr("Preview in"), m_formMenu);    m_formMenu->insertMenu(m_actionManager->previewFormAction(), previewSubMenu);    foreach (QAction *action, m_actionManager->styleActions()->actions()) {        previewSubMenu->addAction(action);    }    m_toolMenu = m_globalMenuBar->addMenu(tr("&Tools"));    m_toolMenu->addSeparator();    m_windowMenu = m_globalMenuBar->addMenu(tr("&Window"));    foreach (QAction *action, m_actionManager->windowActions()->actions()) {        m_windowMenu->addAction(action);    }    m_helpMenu = m_globalMenuBar->addMenu(tr("&Help"));    foreach (QAction *action, m_actionManager->helpActions()->actions()) {        m_helpMenu->addAction(action);    }    QDesignerToolWindow *tw = new QDesignerWidgetBox(this);    tw->setObjectName(QLatin1String("qt_designer_widgetbox"));    addToolWindow(tw);    tw = new QDesignerObjectInspector(this);    tw->setObjectName(QLatin1String("qt_designer_objectinspector"));    addToolWindow(tw);    tw = new QDesignerPropertyEditor(this);    tw->setObjectName(QLatin1String("qt_designer_propertyeditor"));    addToolWindow(tw);    tw = new QDesignerSignalSlotEditor(this);    tw->setObjectName(QLatin1String("qt_designer_signalsloteditor"));    addToolWindow(tw);    tw = new QDesignerResourceEditor(this);    tw->setObjectName(QLatin1String("qt_designer_resourceeditor"));    addToolWindow(tw);    tw = new QDesignerActionEditor(this);    tw->setObjectName(QLatin1String("qt_designer_actioneditor"));    addToolWindow(tw);    m_integration = new qdesigner_internal::QDesignerIntegration(core(), this);    // create the toolbars    m_fileToolBar = new QToolBar;    m_fileToolBar->setObjectName(QLatin1String("fileToolBar"));    m_fileToolBar->setWindowTitle(tr("File"));    foreach (QAction *action, m_actionManager->fileActions()->actions()) {        if (action->icon().isNull() == false)            m_fileToolBar->addAction(action);    }    m_editToolBar = new QToolBar;    m_editToolBar->setObjectName(QLatin1String("editToolBar"));    m_editToolBar->setWindowTitle(tr("Edit"));    foreach (QAction *action, m_actionManager->editActions()->actions()) {        if (action->icon().isNull() == false)            m_editToolBar->addAction(action);    }    m_toolToolBar = new QToolBar;    m_toolToolBar->setObjectName(QLatin1String("toolsToolBar"));    m_toolToolBar->setWindowTitle(tr("Tools"));    foreach (QAction *action, m_actionManager->toolActions()->actions()) {        if (action->icon().isNull() == false)            m_toolToolBar->addAction(action);    }    m_formToolBar = new QToolBar;    m_formToolBar->setObjectName(QLatin1String("formToolBar"));    m_formToolBar->setWindowTitle(tr("Form"));    foreach (QAction *action, m_actionManager->formActions()->actions()) {        if (action->icon().isNull() == false)            m_formToolBar->addAction(action);    }    QMenu *toolbarMenu = m_toolMenu->addMenu(tr("Toolbars"));    toolbarMenu->addAction(m_fileToolBar->toggleViewAction());    toolbarMenu->addAction(m_editToolBar->toggleViewAction());    toolbarMenu->addAction(m_toolToolBar->toggleViewAction());    toolbarMenu->addAction(m_formToolBar->toggleViewAction());    emit initialized();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?