formwindow.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 2,326 行 · 第 1/5 页
CPP
2,326 行
/******************************************************************************** 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.******************************************************************************//*TRANSLATOR qdesigner_internal::FormWindow*/#include "formwindow.h"#include "formeditor.h"#include "formwindow_dnditem.h"#include "formwindow_widgetstack.h"#include "formwindowcursor.h"#include "formwindowmanager.h"#include "tool_widgeteditor.h"#include "widgetselection.h"// shared#include <metadatabase_p.h>#include <qdesigner_tabwidget_p.h>#include <qdesigner_toolbox_p.h>#include <qdesigner_stackedbox_p.h>#include <qdesigner_resource.h>#include <qdesigner_command_p.h>#include <qdesigner_propertycommand_p.h>#include <qdesigner_widget_p.h>#include <qdesigner_utils_p.h>#include <qlayout_widget_p.h>#include <spacer_widget_p.h>#include <invisible_widget_p.h>#include <layoutinfo_p.h>#include <connectionedit_p.h>#include <actionprovider_p.h>#include <QtDesigner/QExtensionManager>#include <QtDesigner/QDesignerWidgetDataBaseInterface>#include <QtDesigner/QDesignerPropertySheetExtension>#include <QtDesigner/QDesignerWidgetFactoryInterface>#include <QtDesigner/QDesignerContainerExtension>#include <QtDesigner/QDesignerTaskMenuExtension>#include <QtDesigner/QDesignerWidgetBoxInterface>#include <QtCore/QtDebug>#include <QtCore/QBuffer>#include <QtCore/QTimer>#include <QtGui/QMenu>#include <QtGui/QClipboard>#include <QtGui/QUndoGroup>#include <QtGui/QScrollArea>#include <QtGui/QRubberBand>#include <QtGui/QApplication>#include <QtGui/QSplitter>#include <QtGui/QMessageBox>#include <QtGui/QPainter>#include <QtGui/QGroupBox>#include <QtGui/QDockWidget>namespace {class BlockSelection{public: BlockSelection(qdesigner_internal::FormWindow *fw) : m_formWindow(fw), m_blocked(m_formWindow->blockSelectionChanged(true)) { } ~BlockSelection() { if (m_formWindow) m_formWindow->blockSelectionChanged(m_blocked); }private: QPointer<qdesigner_internal::FormWindow> m_formWindow; const bool m_blocked;};enum { debugFormWindow = 0 };}namespace qdesigner_internal {// ------------------------ FormWindow::Selection// Maintains a pool of WidgetSelections to be used for selected widgets.class FormWindow::Selection{public: Selection(); ~Selection(); // Clear void clear(); // Also clear out the pool. Call if reparenting of the main container occurs. void clearSelectionPool(); void repaintSelection(QWidget *w); void repaintSelection(); bool isWidgetSelected(QWidget *w) const; QWidgetList selectedWidgets() const; WidgetSelection *addWidget(FormWindow* fw, QWidget *w); // remove widget, return new current widget or 0 QWidget* removeWidget(QWidget *w); void raiseList(const QWidgetList& l); void raiseWidget(QWidget *w); void updateGeometry(QWidget *w);private: typedef QList<WidgetSelection *> SelectionPool; SelectionPool m_selectionPool; typedef QHash<QWidget *, WidgetSelection *> SelectionHash; SelectionHash m_usedSelections;};FormWindow::Selection::Selection(){}FormWindow::Selection::~Selection(){ clearSelectionPool();}void FormWindow::Selection::clear(){ if (!m_usedSelections.empty()) { const SelectionHash::iterator mend = m_usedSelections.end(); for (SelectionHash::iterator it = m_usedSelections.begin(); it != mend; ++it) { it.value()->setWidget(0); } m_usedSelections.clear(); }}void FormWindow::Selection::clearSelectionPool(){ clear(); qDeleteAll(m_selectionPool); m_selectionPool.clear();}WidgetSelection *FormWindow::Selection::addWidget(FormWindow* fw, QWidget *w){ WidgetSelection *rc = m_usedSelections.value(w); if (rc != 0) { rc->show(); return rc; } // find a free one in the pool const SelectionPool::iterator pend = m_selectionPool.end(); for (SelectionPool::iterator it = m_selectionPool.begin(); it != pend; ++it) { if (! (*it)->isUsed()) { rc = *it; break; } } if (rc == 0) { rc = new WidgetSelection(fw); m_selectionPool.push_back(rc); } m_usedSelections.insert(w, rc); rc->setWidget(w); return rc;}QWidget* FormWindow::Selection::removeWidget(QWidget *w){ WidgetSelection *s = m_usedSelections.value(w); if (!s) return w; s->setWidget(0); m_usedSelections.remove(w); if (m_usedSelections.isEmpty()) return 0; return (*m_usedSelections.begin())->widget();}void FormWindow::Selection::repaintSelection(QWidget *w){ if (WidgetSelection *s = m_usedSelections.value(w)) s->update();}void FormWindow::Selection::repaintSelection(){ const SelectionHash::iterator mend = m_usedSelections.end(); for (SelectionHash::iterator it = m_usedSelections.begin(); it != mend; ++it) { it.value()->update(); }}bool FormWindow::Selection::isWidgetSelected(QWidget *w) const{ return m_usedSelections.contains(w);}QWidgetList FormWindow::Selection::selectedWidgets() const{ return m_usedSelections.keys();}void FormWindow::Selection::raiseList(const QWidgetList& l){ const SelectionHash::iterator mend = m_usedSelections.end(); for (SelectionHash::iterator it = m_usedSelections.begin(); it != mend; ++it) { WidgetSelection *w = it.value(); if (l.contains(w->widget())) w->show(); }}void FormWindow::Selection::raiseWidget(QWidget *w){ if (WidgetSelection *s = m_usedSelections.value(w)) s->show();}void FormWindow::Selection::updateGeometry(QWidget *w){ if (WidgetSelection *s = m_usedSelections.value(w)) { s->updateGeometry(); }}// ------------------------ FormWindowFormWindow::FormWindow(FormEditor *core, QWidget *parent, Qt::WindowFlags flags) : FormWindowBase(parent, flags), m_core(core), m_selection(new Selection()), m_widgetStack(0), m_dblClicked(false){ init(); m_cursor = new FormWindowCursor(this, this); core->formWindowManager()->addFormWindow(this); setDirty(false); setAcceptDrops(true);}FormWindow::~FormWindow(){ Q_ASSERT(core() != 0); Q_ASSERT(core()->metaDataBase() != 0); Q_ASSERT(core()->formWindowManager() != 0); core()->formWindowManager()->removeFormWindow(this); core()->metaDataBase()->remove(this); QWidgetList l = widgets(); foreach (QWidget *w, l) core()->metaDataBase()->remove(w); m_widgetStack = 0; m_rubberBand = 0; delete m_selection;}QDesignerFormEditorInterface *FormWindow::core() const{ return m_core;}QDesignerFormWindowCursorInterface *FormWindow::cursor() const{ return m_cursor;}void FormWindow::updateWidgets(){ if (!m_mainContainer) return;}int FormWindow::widgetDepth(const QWidget *w){ int d = -1; while (w && !w->isWindow()) { d++; w = w->parentWidget(); } return d;}bool FormWindow::isChildOf(const QWidget *c, const QWidget *p){ while (c) { if (c == p) return true; c = c->parentWidget(); } return false;}void FormWindow::setCursorToAll(const QCursor &c, QWidget *start){ start->setCursor(c); const QWidgetList widgets = qFindChildren<QWidget*>(start); foreach (QWidget *widget, widgets) { if (!qobject_cast<WidgetHandle*>(widget)) { widget->setCursor(c); } }}void FormWindow::init(){ if (FormWindowManager *manager = qobject_cast<FormWindowManager*> (core()->formWindowManager())) { m_commandHistory = new QUndoStack(this); manager->undoGroup()->addStack(m_commandHistory); } m_blockSelectionChanged = false; m_defaultMargin = INT_MIN; m_defaultSpacing = INT_MIN; QHBoxLayout *layout = new QHBoxLayout(this); layout->setMargin(0); m_widgetStack = new FormWindowWidgetStack(this); connect(m_widgetStack, SIGNAL(currentToolChanged(int)), this, SIGNAL(toolChanged(int))); layout->addWidget(m_widgetStack); m_selectionChangedTimer = new QTimer(this); m_selectionChangedTimer->setSingleShot(true); connect(m_selectionChangedTimer, SIGNAL(timeout()), this, SLOT(selectionChangedTimerDone())); m_checkSelectionTimer = new QTimer(this); m_checkSelectionTimer->setSingleShot(true); connect(m_checkSelectionTimer, SIGNAL(timeout()), this, SLOT(checkSelectionNow())); m_geometryChangedTimer = new QTimer(this); m_geometryChangedTimer->setSingleShot(true); connect(m_geometryChangedTimer, SIGNAL(timeout()), this, SIGNAL(geometryChanged())); m_rubberBand = 0; setFocusPolicy(Qt::StrongFocus); m_mainContainer = 0; m_currentWidget = 0; m_drawRubber = false; connect(m_commandHistory, SIGNAL(indexChanged(int)), this, SLOT(updateDirty())); connect(m_commandHistory, SIGNAL(indexChanged(int)), this, SIGNAL(changed())); connect(m_commandHistory, SIGNAL(indexChanged(int)), this, SLOT(checkSelection())); core()->metaDataBase()->add(this); initializeCoreTools(); QAction *a = new QAction(this); a->setText(tr("Edit contents")); a->setShortcut(tr("F2")); connect(a, SIGNAL(triggered()), this, SLOT(editContents())); addAction(a);}QWidget *FormWindow::mainContainer() const{ return m_mainContainer;}void FormWindow::clearMainContainer(){ if (m_mainContainer) { setCurrentTool(0); core()->metaDataBase()->remove(m_mainContainer); unmanageWidget(m_mainContainer); delete m_mainContainer; m_mainContainer = 0; }}void FormWindow::setMainContainer(QWidget *w){ if (w == m_mainContainer) { // nothing to do return; } clearMainContainer(); m_mainContainer = w; const QSize sz = m_mainContainer->size(); m_mainContainer->setParent(m_widgetStack, 0); m_mainContainer->raise(); m_mainContainer->show(); m_widgetStack->setCurrentTool(m_widgetEditor); setCurrentWidget(m_mainContainer); manageWidget(m_mainContainer); if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), m_mainContainer)) { sheet->setVisible(sheet->indexOf(QLatin1String("windowTitle")), true); sheet->setVisible(sheet->indexOf(QLatin1String("windowIcon")), true); // ### generalize } m_mainContainer->setFocusPolicy(Qt::StrongFocus); m_mainContainer->resize(sz); emit mainContainerChanged(m_mainContainer);}QWidget *FormWindow::findTargetContainer(QWidget *widget) const{ Q_ASSERT(widget); while (QWidget *parentWidget = widget->parentWidget()) { if (LayoutInfo::layoutType(m_core, parentWidget) == LayoutInfo::NoLayout && isManaged(widget)) return widget; widget = parentWidget; } return mainContainer();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?