📄 formwindowmanager.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 "formwindowmanager.h"#include "formwindow_dnditem.h"#include "widgetdatabase_p.h"#include "iconloader_p.h"#include "widgetselection.h"#include "qdesigner_resource.h"#include "connectionedit_p.h"#include <QtDesigner/QtDesigner>#include <QtDesigner/private/qtundo_p.h>#include <qdesigner_promotedwidget_p.h>#include <qdesigner_command_p.h>#include <layoutinfo_p.h>#include <QtGui/QAction>#include <QtGui/QLayout>#include <QtGui/QSplitter>#include <QtGui/QMouseEvent>#include <QtGui/QApplication>#include <QtGui/QIcon>#include <QtGui/QBitmap>#include <QtGui/QPainter>#include <QtGui/QSizeGrip>#include <QtGui/QAbstractButton>#include <QtGui/QToolBox>#include <QtGui/QMainWindow>#include <QtGui/QMenuBar>#include <QtGui/QClipboard>#include <QtGui/QWorkspace>#include <QtGui/QDesktopWidget>#include <QtCore/qdebug.h>using namespace qdesigner_internal;static QString whatsThisFrom(const QString &str){ Q_UNUSED(str); /// ### implement me! return str;}FormWindowManager::FormWindowManager(QDesignerFormEditorInterface *core, QObject *parent) : QDesignerFormWindowManagerInterface(parent), m_core(core), m_activeFormWindow(0){ m_layoutChilds = false; m_savedContextMenuPolicy = Qt::NoContextMenu; setupActions(); qApp->installEventFilter(this); // DnD stuff m_last_widget_under_mouse = 0; m_last_form_under_mouse = 0; m_widget_box_under_mouse = 0;}FormWindowManager::~FormWindowManager(){ qDeleteAll(m_formWindows);}QDesignerFormEditorInterface *FormWindowManager::core() const{ return m_core;}QDesignerFormWindowInterface *FormWindowManager::activeFormWindow() const{ return m_activeFormWindow;}int FormWindowManager::formWindowCount() const{ return m_formWindows.size();}QDesignerFormWindowInterface *FormWindowManager::formWindow(int index) const{ return m_formWindows.at(index);}static bool isMouseMoveOrRelease(QEvent *e){ return e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseMove;}bool FormWindowManager::eventFilter(QObject *o, QEvent *e){ if (#ifdef Q_WS_X11 o == m_core->topLevel() &&#endif !m_drag_item_list.isEmpty() && isMouseMoveOrRelease(e)) { // We're dragging QMouseEvent *me = static_cast<QMouseEvent*>(e); me->accept(); if (me->type() == QEvent::MouseButtonRelease) endDrag(me->globalPos()); else setItemsPos(me->globalPos()); return true; } if (!o->isWidgetType()) return false; QWidget *widget = static_cast<QWidget*>(o); if (qobject_cast<WidgetHandle*>(widget)) { // ### remove me return false; } FormWindow *fw = FormWindow::findFormWindow(widget); if (fw == 0) { return false; } if (QWidget *managedWidget = findManagedWidget(fw, widget)) { switch (e->type()) { case QEvent::Hide: { if (widget == managedWidget && fw->isWidgetSelected(managedWidget)) fw->hideSelection(widget); } break; case QEvent::WindowActivate: { if (fw->parentWidget()->isWindow() && fw->isMainContainer(managedWidget) && activeFormWindow() != fw) { setActiveFormWindow(fw); } } break; case QEvent::WindowDeactivate: { fw->repaintSelection(); } break; case QEvent::KeyPress: { QKeyEvent *ke = static_cast<QKeyEvent*>(e); if (ke->key() == Qt::Key_Escape) { ke->accept(); return true; } } // don't break... default: { if (fw->handleEvent(widget, managedWidget, e)) { return true; } } break; } // end switch } return false;}void FormWindowManager::addFormWindow(QDesignerFormWindowInterface *w){ FormWindow *formWindow = qobject_cast<FormWindow*>(w); if (!formWindow || m_formWindows.contains(formWindow)) return; connect(formWindow, SIGNAL(selectionChanged()), this, SLOT(slotUpdateActions())); connect(formWindow->commandHistory(), SIGNAL(commandExecuted()), this, SLOT(slotUpdateActions())); connect(formWindow, SIGNAL(toolChanged(int)), this, SLOT(slotUpdateActions())); m_formWindows.append(formWindow); emit formWindowAdded(formWindow);}void FormWindowManager::removeFormWindow(QDesignerFormWindowInterface *w){ FormWindow *formWindow = qobject_cast<FormWindow*>(w); int idx = m_formWindows.indexOf(formWindow); if (!formWindow || idx == -1) return; formWindow->disconnect(this); m_formWindows.removeAt(idx); emit formWindowRemoved(formWindow); if (formWindow == m_activeFormWindow) setActiveFormWindow(0);}void FormWindowManager::setActiveFormWindow(QDesignerFormWindowInterface *w){ FormWindow *formWindow = qobject_cast<FormWindow*>(w); if (formWindow == m_activeFormWindow) return; FormWindow *old = m_activeFormWindow; m_activeFormWindow = formWindow; slotUpdateActions(); if (m_activeFormWindow) { m_activeFormWindow->repaintSelection(); if (old) old->repaintSelection(); } emit activeFormWindowChanged(m_activeFormWindow); if (m_activeFormWindow) { m_activeFormWindow->emitSelectionChanged(); m_activeFormWindow->commandHistory()->setCurrent(); QWidget *parent = m_activeFormWindow->parentWidget(); QWorkspace *workspace = 0; while (parent != 0) { if ((workspace = qobject_cast<QWorkspace*>(parent))) break; parent = parent->parentWidget(); } if (workspace != 0) workspace->setActiveWindow(m_activeFormWindow->parentWidget()); }}QWidget *FormWindowManager::findManagedWidget(FormWindow *fw, QWidget *w){ while (w && w != fw) { if (fw->isManaged(w)) break; w = w->parentWidget(); } QWidget *parent = w->parentWidget(); if (parent != 0 && qobject_cast<QDesignerPromotedWidget*>(parent) != 0) w = parent; return w;}void FormWindowManager::setupActions(){ m_actionCut = new QAction(createIconSet(QLatin1String("editcut.png")), tr("Cu&t"), this); m_actionCut->setShortcut(Qt::CTRL + Qt::Key_X); m_actionCut->setStatusTip(tr("Cuts the selected widgets and puts them on the clipboard")); m_actionCut->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Cut"))); connect(m_actionCut, SIGNAL(triggered()), this, SLOT(slotActionCutActivated())); m_actionCut->setEnabled(false); m_actionCopy = new QAction(createIconSet(QLatin1String("editcopy.png")), tr("&Copy"), this); m_actionCopy->setShortcut(Qt::CTRL + Qt::Key_C); m_actionCopy->setStatusTip(tr("Copies the selected widgets to the clipboard")); m_actionCopy->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Copy"))); connect(m_actionCopy, SIGNAL(triggered()), this, SLOT(slotActionCopyActivated())); m_actionCopy->setEnabled(false); m_actionPaste = new QAction(createIconSet(QLatin1String("editpaste.png")), tr("&Paste"), this); m_actionPaste->setShortcut(Qt::CTRL + Qt::Key_V); m_actionPaste->setStatusTip(tr("Pastes the clipboard's contents")); m_actionPaste->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Paste"))); connect(m_actionPaste, SIGNAL(triggered()), this, SLOT(slotActionPasteActivated())); m_actionPaste->setEnabled(false); m_actionDelete = new QAction(tr("&Delete"), this); m_actionDelete->setStatusTip(tr("Deletes the selected widgets")); m_actionDelete->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Delete"))); connect(m_actionDelete, SIGNAL(triggered()), this, SLOT(slotActionDeleteActivated())); m_actionDelete->setEnabled(false); m_actionSelectAll = new QAction(tr("Select &All"), this); m_actionSelectAll->setShortcut(Qt::CTRL + Qt::Key_A); m_actionSelectAll->setStatusTip(tr("Selects all widgets")); m_actionSelectAll->setWhatsThis(whatsThisFrom(QLatin1String("Edit|Select All"))); connect(m_actionSelectAll, SIGNAL(triggered()), this, SLOT(slotActionSelectAllActivated())); m_actionSelectAll->setEnabled(false); m_actionRaise = new QAction(createIconSet(QLatin1String("editraise.png")), tr("Bring to &Front"), this); m_actionRaise->setStatusTip(tr("Raises the selected widgets")); m_actionRaise->setWhatsThis(tr("Raises the selected widgets")); connect(m_actionRaise, SIGNAL(triggered()), this, SLOT(slotActionRaiseActivated())); m_actionRaise->setEnabled(false); m_actionLower = new QAction(createIconSet(QLatin1String("editlower.png")), tr("Send to &Back"), this); m_actionLower->setStatusTip(tr("Lowers the selected widgets")); m_actionLower->setWhatsThis(tr("Lowers the selected widgets")); connect(m_actionLower, SIGNAL(triggered()), this, SLOT(slotActionLowerActivated())); m_actionLower->setEnabled(false); m_actionAdjustSize = new QAction(createIconSet(QLatin1String("adjustsize.png")), tr("Adjust &Size"), this); m_actionAdjustSize->setShortcut(Qt::CTRL + Qt::Key_J); m_actionAdjustSize->setStatusTip(tr("Adjusts the size of the selected widget")); m_actionAdjustSize->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Adjust Size"))); connect(m_actionAdjustSize, SIGNAL(triggered()), this, SLOT(slotActionAdjustSizeActivated())); m_actionAdjustSize->setEnabled(false); m_actionHorizontalLayout = new QAction(createIconSet(QLatin1String("edithlayout.png")), tr("Lay Out &Horizontally"), this); m_actionHorizontalLayout->setShortcut(Qt::CTRL + Qt::Key_1); m_actionHorizontalLayout->setStatusTip(tr("Lays out the selected widgets horizontally")); m_actionHorizontalLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Horizontally"))); connect(m_actionHorizontalLayout, SIGNAL(triggered()), this, SLOT(slotActionHorizontalLayoutActivated())); m_actionHorizontalLayout->setEnabled(false); m_actionVerticalLayout = new QAction(createIconSet(QLatin1String("editvlayout.png")), tr("Lay Out &Vertically"), this); m_actionVerticalLayout->setShortcut(Qt::CTRL + Qt::Key_2); m_actionVerticalLayout->setStatusTip(tr("Lays out the selected widgets vertically")); m_actionVerticalLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Vertically"))); connect(m_actionVerticalLayout, SIGNAL(triggered()), this, SLOT(slotActionVerticalLayoutActivated())); m_actionVerticalLayout->setEnabled(false); m_actionGridLayout = new QAction(createIconSet(QLatin1String("editgrid.png")), tr("Lay Out in a &Grid"), this); m_actionGridLayout->setShortcut(Qt::CTRL + Qt::Key_5); m_actionGridLayout->setStatusTip(tr("Lays out the selected widgets in a grid")); m_actionGridLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out in a Grid"))); connect(m_actionGridLayout, SIGNAL(triggered()), this, SLOT(slotActionGridLayoutActivated())); m_actionGridLayout->setEnabled(false); m_actionSplitHorizontal = new QAction(createIconSet(QLatin1String("editvlayoutsplit.png")), tr("Lay Out Horizontally in S&plitter"), this); m_actionSplitHorizontal->setShortcut(Qt::CTRL + Qt::Key_3); m_actionSplitHorizontal->setStatusTip(tr("Lays out the selected widgets horizontally in a splitter")); m_actionSplitHorizontal->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Horizontally in Splitter"))); connect(m_actionSplitHorizontal, SIGNAL(triggered()), this, SLOT(slotActionSplitHorizontalActivated())); m_actionSplitHorizontal->setEnabled(false); m_actionSplitVertical = new QAction(createIconSet(QLatin1String("edithlayoutsplit.png")), tr("Lay Out Vertically in Sp&litter"), this); m_actionSplitVertical->setShortcut(Qt::CTRL + Qt::Key_4); m_actionSplitVertical->setStatusTip(tr("Lays out the selected widgets vertically in a splitter")); m_actionSplitVertical->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Lay Out Vertically in Splitter"))); connect(m_actionSplitVertical, SIGNAL(triggered()), this, SLOT(slotActionSplitVerticalActivated())); m_actionSplitVertical->setEnabled(false); m_actionBreakLayout = new QAction(createIconSet(QLatin1String("editbreaklayout.png")), tr("&Break Layout"), this); m_actionBreakLayout->setShortcut(Qt::CTRL + Qt::Key_0); m_actionBreakLayout->setStatusTip(tr("Breaks the selected layout")); m_actionBreakLayout->setWhatsThis(whatsThisFrom(QLatin1String("Layout|Break Layout"))); connect(m_actionBreakLayout, SIGNAL(triggered()), this, SLOT(slotActionBreakLayoutActivated())); m_actionBreakLayout->setEnabled(false); m_actionUndo = QtUndoManager::manager()->createUndoAction(this); m_actionUndo->setEnabled(false); m_actionRedo = QtUndoManager::manager()->createRedoAction(this); m_actionRedo->setEnabled(false);}void FormWindowManager::slotActionCutActivated(){ m_activeFormWindow->cut();}void FormWindowManager::slotActionCopyActivated(){ m_activeFormWindow->copy(); slotUpdateActions();}void FormWindowManager::slotActionPasteActivated(){ m_activeFormWindow->paste();}void FormWindowManager::slotActionDeleteActivated(){ m_activeFormWindow->deleteWidgets();}void FormWindowManager::slotActionLowerActivated(){ m_activeFormWindow->lowerWidgets();}void FormWindowManager::slotActionRaiseActivated(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -