qdesigner_actions.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,325 行 · 第 1/3 页
CPP
1,325 行
bool QDesignerActions::openForm(QWidget *parent){#ifdef NONMODAL_PREVIEW closePreview();#endif const QString extension = getFileExtension(core()); const QStringList fileNames = QFileDialog::getOpenFileNames(parent, tr("Open Form"), m_openDirectory, tr("Designer UI files (*.%1);;All Files (*)").arg(extension), 0, QFileDialog::DontUseSheet); if (fileNames.isEmpty()) return false; bool atLeastOne = false; foreach (QString fileName, fileNames) { if (readInForm(fileName) && !atLeastOne) atLeastOne = true; } return atLeastOne;}bool QDesignerActions::saveFormAs(QDesignerFormWindowInterface *fw){ const QString extension = getFileExtension(core()); QString dir = fw->fileName(); if (dir.isEmpty()) { do { // Build untitled name if (!m_saveDirectory.isEmpty()) { dir = m_saveDirectory; break; } if (!m_openDirectory.isEmpty()) { dir = m_openDirectory; break; } dir = QDir::current().absolutePath(); } while (false); dir += QDir::separator(); dir += QLatin1String("untitled."); dir += extension; } QString saveFile; while (1) { saveFile = QFileDialog::getSaveFileName(fw, tr("Save form as"), dir, tr("Designer UI files (*.%1);;All Files (*)").arg(extension), 0, QFileDialog::DontConfirmOverwrite); if (saveFile.isEmpty()) return false; const QFileInfo fInfo(saveFile); if (fInfo.suffix().isEmpty() && !fInfo.fileName().endsWith(QLatin1Char('.'))) saveFile.append(QLatin1Char('.')).append(extension); const QFileInfo fi(saveFile); if (!fi.exists()) break; if (QMessageBox::warning(fw, tr("Save"), tr("%1 already exists.\nDo you want to replace it?") .arg(fi.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) break; dir = saveFile; } fw->setFileName(saveFile); return writeOutForm(fw, saveFile);}void QDesignerActions::saveForm(){ if (QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow()) { if (saveForm(fw)) showStatusBarMessage(savedMessage(fw->fileName())); }}void QDesignerActions::saveAllForms(){ QString fileNames; QDesignerFormWindowManagerInterface *formWindowManager = core()->formWindowManager(); if (const int totalWindows = formWindowManager->formWindowCount()) { const QString separator = QLatin1String(", "); for (int i = 0; i < totalWindows; ++i) { QDesignerFormWindowInterface *fw = formWindowManager->formWindow(i); if (fw && fw->isDirty()) { formWindowManager->setActiveFormWindow(fw); if (saveForm(fw)) { if (!fileNames.isEmpty()) fileNames += separator; fileNames += QFileInfo(fw->fileName()).fileName(); } else { break; } } } } if (!fileNames.isEmpty()) { showStatusBarMessage(savedMessage(fileNames)); }}bool QDesignerActions::saveForm(QDesignerFormWindowInterface *fw){ bool ret; if (fw->fileName().isEmpty()) ret = saveFormAs(fw); else ret = writeOutForm(fw, fw->fileName()); return ret;}void QDesignerActions::closeForm(){#ifdef NONMODAL_PREVIEW if (closePreview()) return;#endif if (QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow()) if (QWidget *parent = fw->parentWidget()) { if (QMdiSubWindow *mdiSubWindow = qobject_cast<QMdiSubWindow *>(parent->parentWidget())) { mdiSubWindow->close(); } else { parent->close(); } }}void QDesignerActions::saveFormAs(){ if (QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow()) { if (saveFormAs(fw)) showStatusBarMessage(savedMessage(fw->fileName())); }}void QDesignerActions::saveFormAsTemplate(){ if (QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow()) { SaveFormAsTemplate dlg(fw, fw->window()); dlg.exec(); }}void QDesignerActions::notImplementedYet(){ QMessageBox::information(core()->topLevel(), tr("Designer"), tr("Feature not implemented yet!"));}void QDesignerActions::previewFormLater(QAction *action){ qRegisterMetaType<QAction*>("QAction*"); QMetaObject::invokeMethod(this, "previewForm", Qt::QueuedConnection, Q_ARG(QAction*, action));}bool QDesignerActions::closePreview(){ if (m_previewWidget) { m_previewWidget->close(); return true; } return false;}void QDesignerActions::previewForm(QAction *action){#ifdef NONMODAL_PREVIEW closePreview();#endif QDesignerFormWindowInterface *fw = core()->formWindowManager()->activeFormWindow(); if (!fw) return; // Get style stored in action if any QString styleName; if (action) { const QVariant data = action->data(); if (data.type() == QVariant::String) styleName = data.toString(); } // create and have form builder display errors. QWidget *widget = qdesigner_internal::QDesignerFormBuilder::createPreview(fw, styleName); if (!widget) return;#ifdef Q_WS_WIN Qt::WindowFlags windwowFlags = (widget->windowType() == Qt::Window) ? Qt::Window | Qt::WindowMaximizeButtonHint : Qt::WindowFlags(Qt::Dialog);#else // Only Dialogs have close buttons on mac // On linux we don't want additional item on task bar and don't want minimize button, we want the preview to be on top Qt::WindowFlags windwowFlags = Qt::Dialog;#endif // Install filter for Escape key widget->setParent(fw->window(), windwowFlags);#ifdef NONMODAL_PREVIEW connect(fw, SIGNAL(changed()), widget, SLOT(close()));#else // Cannot do this on the Mac as the dialog would have no close button widget->setWindowModality(Qt::ApplicationModal);#endif // Position over form window widget->setAttribute(Qt::WA_DeleteOnClose, true); widget->move(fw->mapToGlobal(QPoint(10, 10))); widget->installEventFilter(this); widget->show(); m_previewWidget = widget;#ifdef NONMODAL_PREVIEW updateCloseAction();#endif}void QDesignerActions::fixActionContext(){ QList<QAction*> actions; actions += m_fileActions->actions(); actions += m_editActions->actions(); actions += m_toolActions->actions(); actions += m_formActions->actions(); actions += m_windowActions->actions(); actions += m_helpActions->actions(); foreach (QAction *a, actions) { a->setShortcutContext(Qt::ApplicationShortcut); }}bool QDesignerActions::readInForm(const QString &fileName){ QString fn = fileName; // First make sure that we don't have this one open already. QDesignerFormWindowManagerInterface *formWindowManager = core()->formWindowManager(); const int totalWindows = formWindowManager->formWindowCount(); for (int i = 0; i < totalWindows; ++i) { QDesignerFormWindowInterface *w = formWindowManager->formWindow(i); if (w->fileName() == fn) { w->raise(); formWindowManager->setActiveFormWindow(w); addRecentFile(fn); return true; } } // Otherwise load it. do { QString errorMessage; if (workbench()->openForm(fn, &errorMessage)) { addRecentFile(fn); m_openDirectory = QFileInfo(fn).absolutePath(); return true; } else { // prompt to reload QMessageBox box(QMessageBox::Warning, tr("Read error"), tr("%1\nDo you want to update the file location or generate a new form?").arg(errorMessage), QMessageBox::Cancel, core()->topLevel()); QPushButton *updateButton = box.addButton(tr("&Update"), QMessageBox::ActionRole); QPushButton *newButton = box.addButton(tr("&New Form"), QMessageBox::ActionRole); box.exec(); if (box.clickedButton() == box.button(QMessageBox::Cancel)) return false; if (box.clickedButton() == updateButton) { const QString extension = getFileExtension(core()); fn = QFileDialog::getOpenFileName(core()->topLevel(), tr("Open Form"), m_openDirectory, tr("Designer UI files (*.%1);;All Files (*)").arg(extension), 0, QFileDialog::DontUseSheet); if (fn.isEmpty()) return false; } else if (box.clickedButton() == newButton) { // If the file does not exist, but its directory, is valid, open the template with the editor file name set to it. // (called from command line). QString newFormFileName; const QFileInfo fInfo(fn); if (!fInfo.exists()) { // Normalize file name const QString directory = fInfo.absolutePath(); if (QDir(directory).exists()) { newFormFileName = directory; newFormFileName += QLatin1Char('/'); newFormFileName += fInfo.fileName(); } } showNewFormDialog(newFormFileName); return false; } } } while (true); return true;}static QString createBackup(const QString &fileName){ const QString suffix = QLatin1String(".bak"); QString backupFile = fileName + suffix; QFileInfo fi(backupFile); int i = 0; while (fi.exists()) { backupFile = fileName + suffix + QString::number(++i); fi.setFile(backupFile); } if (QFile::copy(fileName, backupFile)) return backupFile; return QString();}static void removeBackup(const QString &backupFile){ if (!backupFile.isEmpty()) QFile::remove(backupFile);}bool QDesignerActions::writeOutForm(QDesignerFormWindowInterface *fw, const QString &saveFile){ Q_ASSERT(fw && !saveFile.isEmpty()); QString backupFile; QFileInfo fi(saveFile); if (fi.exists()) backupFile = createBackup(saveFile); const QByteArray utf8Array = fw->contents().toUtf8(); m_workbench->updateBackup(fw); QFile f(saveFile); while (!f.open(QFile::WriteOnly)) { QMessageBox box(QMessageBox::Warning, tr("Save Form?"), tr("Could not open file"), QMessageBox::NoButton, fw); box.setWindowModality(Qt::WindowModal); box.setInformativeText(tr("The file, %1, could not be opened" "\nReason: %2" "\nWould you like to retry or change your file?") .arg(f.fileName()).arg(f.errorString())); QPushButton *retryButton = box.addButton(QMessageBox::Retry); retryButton->setDefault(true); QPushButton *switchButton = box.addButton(tr("Select New File"), QMessageBox::AcceptRole); QPushButton *cancelButton = box.addButton(QMessageBox::Cancel); box.exec(); if (box.clickedButton() == cancelButton) { removeBackup(backupFile); return false; } else if (box.clickedButton() == switchButton) { QString extension = getFileExtension(core()); const QString fileName = QFileDialog::getSaveFileName(fw, tr("Save form as"), QDir::current().absolutePath(), QLatin1String("*.") + extension); if (fileName.isEmpty()) { removeBackup(backupFile); return false; } if (f.fileName() != fileName) { removeBackup(backupFile); fi.setFile(fileName); backupFile = QString(); if (fi.exists()) backupFile = createBackup(fileName); } f.setFileName(fileName); fw->setFileName(fileName); } // loop back around... } while (f.write(utf8Array, utf8Array.size()) != utf8Array.size()) { QMessageBox box(QMessageBox::Warning, tr("Save Form?"), tr("Could not write file"), QMessageBox::Retry|QMessageBox::Cancel, fw); box.setWindowModality(Qt::WindowModal); box.setInformativeText(tr("It was not possible to write the entire file, %1, to disk." "\nReason:%2\nWould you like to retry?") .arg(f.fileName()).arg(f.errorString())); box.setDefaultButton(QMessageBox::Retry); switch (box.exec()) { case QMessageBox::Retry: f.resize(0); break; default: return false; } } f.close(); removeBackup(backupFile); addRecentFile(saveFile); m_saveDirectory = QFileInfo(f).absolutePath(); fw->setDirty(false); fw->parentWidget()->setWindowModified(false); return true;}void QDesignerActions::shutdown(){ // Follow the idea from the Mac, i.e. send the Application a close event // and if it's accepted, quit. QCloseEvent ev; QApplication::sendEvent(qDesigner, &ev); if (ev.isAccepted()) qDesigner->quit();}void QDesignerActions::activeFormWindowChanged(QDesignerFormWindowInterface *formWindow){ const bool enable = formWindow != 0;#ifdef NONMODAL_PREVIEW closePreview();#endif m_saveFormAction->setEnabled(enable); m_saveFormAsAction->setEnabled(enable); m_saveAllFormsAction->setEnabled(enable); m_saveFormAsTemplateAction->setEnabled(enable); m_closeFormAction->setEnabled(enable); m_closeFormAction->setEnabled(enable); m_editWidgetsAction->setEnabled(enable); m_formSettings->setEnabled(enable); m_previewFormAction->setEnabled(enable); m_styleActions->setEnabled(enable);}void QDesignerActions::updateRecentFileActions(){ QDesignerSettings settings; QStringList files = settings.recentFilesList();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?