From 37a5b8dec14daea7f246b69f261dd22253111ce9 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann <Thomas.Hartmann@nokia.com> Date: Tue, 27 Mar 2012 14:09:53 +0200 Subject: [PATCH] Add a scratch file wizard Commit d86b2abc0bc71f8748356c1a0eb7eb7c60226873 was accidentally reverted. See: d86b2abc0bc71f8748356c1a0eb7eb7c60226873 for details Change-Id: I0efff4126f3504661957529ce98a1fc1ea169a00 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com> --- src/plugins/texteditor/texteditorplugin.cpp | 55 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index d10a75d65f1..fa3a3a70cc0 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -65,6 +65,8 @@ #include <QtPlugin> #include <QMainWindow> #include <QShortcut> +#include <QDir> +#include <QTemporaryFile> using namespace TextEditor; using namespace TextEditor::Internal; @@ -98,6 +100,54 @@ TextEditorPlugin *TextEditorPlugin::instance() return m_instance; } +static const char wizardCategoryC[] = "U.General"; + +static inline QString wizardDisplayCategory() +{ + return TextEditorPlugin::tr("General"); +} + +// A wizard that quickly creates a scratch buffer +// based on a temporary file without prompting for a path. +class ScratchFileWizard : public Core::IWizard +{ +public: + virtual WizardKind kind() const { return FileWizard; } + virtual QIcon icon() const { return QIcon(); } + virtual QString description() const + { return TextEditorPlugin::tr("Creates a scratch buffer using a temporary file."); } + virtual QString displayName() const + { return TextEditorPlugin::tr("Scratch Buffer"); } + virtual QString id() const + { return QLatin1String("Z.ScratchFile"); } + virtual QString category() const + { return QLatin1String(wizardCategoryC); } + virtual QString displayCategory() const + { return wizardDisplayCategory(); } + virtual QString descriptionImage() const + { return QString(); } + virtual Core::FeatureSet requiredFeatures() const + { return Core::FeatureSet(); } + virtual WizardFlags flags() const + { return Core::IWizard::PlatformIndependent; } + + virtual void runWizard(const QString &path, QWidget *parent, const QString &platform); +}; + +void ScratchFileWizard::runWizard(const QString &, QWidget *, const QString &) +{ + QString tempPattern = QDir::tempPath(); + if (!tempPattern.endsWith(QLatin1Char('/'))) + tempPattern += QLatin1Char('/'); + tempPattern += QLatin1String("scratchXXXXXX.txt"); + QTemporaryFile file(tempPattern); + file.setAutoRemove(false); + QTC_ASSERT(file.open(), return; ); + file.close(); + Core::EditorManager *em = Core::EditorManager::instance(); + em->openEditor(file.fileName(), Core::Id(), Core::EditorManager::ModeSwitch); +} + // ExtensionSystem::PluginInterface bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage) { @@ -110,14 +160,15 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe wizardParameters.setDescription(tr("Creates a text file. The default file extension is <tt>.txt</tt>. " "You can specify a different extension as part of the filename.")); wizardParameters.setDisplayName(tr("Text File")); - wizardParameters.setCategory(QLatin1String("U.General")); - wizardParameters.setDisplayCategory(tr("General")); + wizardParameters.setCategory(QLatin1String(wizardCategoryC)); + wizardParameters.setDisplayCategory(wizardDisplayCategory()); wizardParameters.setFlags(Core::IWizard::PlatformIndependent); TextFileWizard *wizard = new TextFileWizard(QLatin1String(Constants::C_TEXTEDITOR_MIMETYPE_TEXT), QLatin1String("text$"), wizardParameters); // Add text file wizard addAutoReleasedObject(wizard); + addAutoReleasedObject(new ScratchFileWizard); m_settings = new TextEditorSettings(this); -- GitLab