qdesigner_actions.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,325 行 · 第 1/3 页
CPP
1,325 行
const int originalSize = files.size(); int numRecentFiles = qMin(files.size(), int(MaxRecentFiles)); const QList<QAction *> recentFilesActs = m_recentFilesActions->actions(); for (int i = 0; i < numRecentFiles; ++i) { const QFileInfo fi(files[i]); // If the file doesn't exist anymore, just remove it from the list so // people don't get confused. if (!fi.exists()) { files.removeAt(i); --i; numRecentFiles = qMin(files.size(), int(MaxRecentFiles)); continue; } const QString text = fi.fileName(); recentFilesActs[i]->setText(text); recentFilesActs[i]->setIconText(files[i]); recentFilesActs[i]->setVisible(true); } for (int j = numRecentFiles; j < MaxRecentFiles; ++j) recentFilesActs[j]->setVisible(false); // If there's been a change, right it back if (originalSize != files.size()) settings.setRecentFilesList(files);}void QDesignerActions::openRecentForm(){ if (const QAction *action = qobject_cast<const QAction *>(sender())) { if (!readInForm(action->iconText())) updateRecentFileActions(); // File doesn't exist, remove it from settings }}void QDesignerActions::clearRecentFiles(){ QDesignerSettings settings; settings.setRecentFilesList(QStringList()); updateRecentFileActions();}QActionGroup *QDesignerActions::recentFilesActions() const{ return m_recentFilesActions;}void QDesignerActions::addRecentFile(const QString &fileName){ QDesignerSettings settings; QStringList files = settings.recentFilesList(); files.removeAll(fileName); files.prepend(fileName); while (files.size() > MaxRecentFiles) files.removeLast(); settings.setRecentFilesList(files); updateRecentFileActions();}QAction *QDesignerActions::closeFormAction() const{ return m_closeFormAction;}QAction *QDesignerActions::minimizeAction() const{ return m_minimizeAction;}void QDesignerActions::showDesignerHelp(){ showHelp(QLatin1String("designer-manual.html"));}void QDesignerActions::showWhatsNew(){ showHelp(QLatin1String("qt4-designer.html"));}void QDesignerActions::showHelp(const QString &url){ if (!m_assistantClient) m_assistantClient = new QAssistantClient(QLibraryInfo::location(QLibraryInfo::BinariesPath), this); QString filePath = QLibraryInfo::location(QLibraryInfo::DocumentationPath); filePath += QLatin1String("/html/"); filePath += url; QString cleanFilePath(filePath); const int index = cleanFilePath.lastIndexOf(QLatin1Char('#')); if (index != -1) cleanFilePath.remove(index, cleanFilePath.size() - index); if (!QFile::exists(cleanFilePath)) { filePath = QLibraryInfo::location(QLibraryInfo::DocumentationPath); filePath += QLatin1String("/html/designer-manual.html"); } m_assistantClient->showPage(filePath);}void QDesignerActions::aboutDesigner(){ VersionDialog mb(core()->topLevel()); mb.setWindowTitle(tr("About Qt Designer")); if (mb.exec()) { OublietteView *oubliette = new OublietteView; oubliette->setAttribute(Qt::WA_DeleteOnClose); oubliette->setMinimumSize(800, 600); oubliette->show(); }}QAction *QDesignerActions::preferencesAction() const{ return m_preferencesAction;}QAction *QDesignerActions::editWidgets() const{ return m_editWidgetsAction;}void QDesignerActions::showWidgetSpecificHelp(){ QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow(); if (!fw) { showDesignerHelp(); return; } QString className; const QString currentPropertyName = core()->propertyEditor()->currentPropertyName(); if (!currentPropertyName.isEmpty()) { QDesignerPropertySheetExtension *ps = qt_extension<QDesignerPropertySheetExtension *>(core()->extensionManager(), core()->propertyEditor()->object()); if (!ps) ps = qt_extension<QDesignerPropertySheetExtension *>(core()->extensionManager(), fw->cursor()->selectedWidget(0)); Q_ASSERT(ps); className = ps->propertyGroup(ps->indexOf(currentPropertyName)); } else { QDesignerWidgetDataBaseInterface *db = core()->widgetDataBase(); QDesignerWidgetDataBaseItemInterface *dbi = db->item(db->indexOfObject(fw->cursor()->selectedWidget(0), true)); className = dbi->name(); } // ### generalize using the Widget Data Base if (className == QLatin1String("Line")) className = QLatin1String("QFrame"); else if (className == QLatin1String("Spacer")) className = QLatin1String("QSpacerItem"); else if (className == QLatin1String("QLayoutWidget")) className = QLatin1String("QLayout"); QString url = className.toLower(); // special case url += QLatin1String(".html"); if (!currentPropertyName.isEmpty()) { url += QLatin1Char('#'); url += currentPropertyName; } showHelp(url);}void QDesignerActions::aboutPlugins(){ PluginDialog dlg(core(), core()->topLevel()); dlg.exec();}void QDesignerActions::showFormSettings(){ QDesignerFormWindowInterface *formWindow = core()->formWindowManager()->activeFormWindow(); QDesignerFormWindow *window = m_workbench->findFormWindow(formWindow); QExtensionManager *mgr = core()->extensionManager(); QDialog *settingsDialog = 0; if (QDesignerLanguageExtension *lang = qt_extension<QDesignerLanguageExtension*>(mgr, core())) settingsDialog = lang->createFormWindowSettingsDialog(formWindow, /*parent=*/ 0); if (! settingsDialog) settingsDialog = new FormWindowSettings(formWindow); QString title = QFileInfo(formWindow->fileName()).fileName(); if (title.isEmpty()) title = window->windowTitle(); settingsDialog->setWindowTitle(tr("Form Settings - %1").arg(title)); if (settingsDialog->exec() && window) { formWindow->setDirty(true); window->updateChanged(); } delete settingsDialog;}bool QDesignerActions::eventFilter(QObject *watched, QEvent *event){ do { if (!watched->isWidgetType()) break; QWidget *previewWindow = qobject_cast<QWidget *>(watched); if (!previewWindow || !previewWindow->isWindow()) break; switch (event->type()) { case QEvent::KeyPress: case QEvent::ShortcutOverride: { const QKeyEvent *keyEvent = static_cast<const QKeyEvent *>(event); const int key = keyEvent->key(); if ((key == Qt::Key_Escape#ifdef Q_WS_MAC || (keyEvent->modifiers() == Qt::ControlModifier && key == Qt::Key_Period)#endif )) { previewWindow->close(); return true; } } break;#ifdef NONMODAL_PREVIEW case QEvent::Close: previewWindow->removeEventFilter(this); m_previewWidget = 0; updateCloseAction(); break;#endif default: break; } } while(false); return QObject::eventFilter(watched, event);}void QDesignerActions::updateCloseAction(){ if (m_previewWidget) { m_closeFormAction->setText(tr("&Close Preview")); } else { m_closeFormAction->setText(tr("&Close Form")); }}void QDesignerActions::backupForms(){ const int count = m_workbench->formWindowCount(); if (!count || !ensureBackupDirectories()) return; QStringList tmpFiles; QMap<QString, QString> backupMap; QDir backupDir(m_backupPath); const bool warningsEnabled = qdesigner_internal::QSimpleResource::setWarningsEnabled(false); for (int i = 0; i < count; ++i) { QDesignerFormWindow *fw = m_workbench->formWindow(i); QDesignerFormWindowInterface *fwi = fw->editor(); QString formBackupName; QTextStream(&formBackupName) << m_backupPath << QDir::separator() << QLatin1String("backup") << i << QLatin1String(".bak"); QString fwn = QDir::convertSeparators(fwi->fileName()); if (fwn.isEmpty()) fwn = fw->windowTitle(); backupMap.insert(fwn, formBackupName); QFile file(formBackupName.replace(m_backupPath, m_backupTmpPath)); if (file.open(QFile::WriteOnly)){ const QByteArray utf8Array = fixResourceFileBackupPath(fwi, backupDir).toUtf8(); if (file.write(utf8Array, utf8Array.size()) != utf8Array.size()) { backupMap.remove(fwn); qdesigner_internal::designerWarning(QObject::tr("The backup file %1 could not be written.").arg(file.fileName())); } else tmpFiles.append(formBackupName); file.close(); } } qdesigner_internal::QSimpleResource::setWarningsEnabled(warningsEnabled); if(!tmpFiles.isEmpty()) { const QStringList backupFiles = backupDir.entryList(QDir::Files); if(!backupFiles.isEmpty()) { QStringListIterator it(backupFiles); while (it.hasNext()) backupDir.remove(it.next()); } QStringListIterator it(tmpFiles); while (it.hasNext()) { const QString tmpName = it.next(); QString name(tmpName); name.replace(m_backupTmpPath, m_backupPath); QFile tmpFile(tmpName); if (!tmpFile.copy(name)) qdesigner_internal::designerWarning(QObject::tr("The backup file %1 could not be written.").arg(name)); tmpFile.remove(); } QDesignerSettings().setBackup(backupMap); }}QString QDesignerActions::fixResourceFileBackupPath(QDesignerFormWindowInterface *fwi, const QDir& backupDir){ const QString content = fwi->contents(); QDomDocument domDoc(QLatin1String("backup")); if(!domDoc.setContent(content)) return content; const QDomNodeList list = domDoc.elementsByTagName(QLatin1String("resources")); if (list.isEmpty()) return content; for (int i = 0; i < list.count(); i++) { const QDomNode node = list.at(i); if (!node.isNull()) { const QDomElement element = node.toElement(); if(!element.isNull() && element.tagName() == QLatin1String("resources")) { QDomNode childNode = element.firstChild(); while (!childNode.isNull()) { QDomElement childElement = childNode.toElement(); if(!childElement.isNull() && childElement.tagName() == QLatin1String("include")) { const QString attr = childElement.attribute(QLatin1String("location")); const QString path = fwi->absoluteDir().absoluteFilePath(attr); childElement.setAttribute(QLatin1String("location"), backupDir.relativeFilePath(path)); } childNode = childNode.nextSibling(); } } } } return domDoc.toString();}QRect QDesignerActions::fixDialogRect(const QRect &rect) const{ QRect frameGeometry; const QRect availableGeometry = QApplication::desktop()->availableGeometry(core()->topLevel()); if (workbench()->mode() == DockedMode) { frameGeometry = core()->topLevel()->frameGeometry(); } else frameGeometry = availableGeometry; QRect dlgRect = rect; dlgRect.moveCenter(frameGeometry.center()); // make sure that parts of the dialog are not outside of screen dlgRect.moveBottom(qMin(dlgRect.bottom(), availableGeometry.bottom())); dlgRect.moveRight(qMin(dlgRect.right(), availableGeometry.right())); dlgRect.moveLeft(qMax(dlgRect.left(), availableGeometry.left())); dlgRect.moveTop(qMax(dlgRect.top(), availableGeometry.top())); return dlgRect;}void QDesignerActions::showStatusBarMessage(const QString &message) const{ if (workbench()->mode() == DockedMode) { QStatusBar *bar = qDesigner->mainWindow()->statusBar(); if (bar && !bar->isHidden()) bar->showMessage(message, 3000); }}void QDesignerActions::setBringAllToFrontVisible(bool visible){ m_bringAllToFrontSeparator->setVisible(visible); m_bringAllToFrontAction->setVisible(visible);}void QDesignerActions::setWindowListSeparatorVisible(bool visible){ m_windowListSeparatorAction->setVisible(visible);}bool QDesignerActions::ensureBackupDirectories() { if (m_backupPath.isEmpty()) { // create names m_backupPath = QDir::homePath(); m_backupPath += QDir::separator(); m_backupPath += QLatin1String(".designer"); m_backupPath += QDir::separator(); m_backupPath += QLatin1String("backup"); m_backupPath = QDir::convertSeparators(m_backupPath ); m_backupTmpPath = m_backupPath; m_backupTmpPath += QDir::separator(); m_backupTmpPath += QLatin1String("tmp"); m_backupTmpPath = QDir::convertSeparators(m_backupTmpPath); } // ensure directories const QDir backupDir(m_backupPath); const QDir backupTmpDir(m_backupTmpPath); if (!backupDir.exists()) { if (!backupDir.mkpath(m_backupPath)) { qdesigner_internal::designerWarning(QObject::tr("The backup directory %1 could not be created.").arg(m_backupPath)); return false; } } if (!backupTmpDir.exists()) { if (!backupTmpDir.mkpath(m_backupTmpPath)) { qdesigner_internal::designerWarning(QObject::tr("The temporary backup directory %1 could not be created.").arg(m_backupTmpPath)); return false; } } return true;}void QDesignerActions::showPreferencesDialog(){ QDesignerSettings settings; Preferences preferences = settings.preferences(); { // It is important that the dialog be deleted before UI mode changes. PreferencesDialog preferencesDialog(workbench()->core()->topLevel()); if (!preferencesDialog.showDialog(preferences)) { return; } } settings.setPreferences(preferences); m_workbench->applyPreferences(preferences);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?