diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp index 4941a8240bf..e4a34d49fb0 100644 --- a/src/plugins/cpaster/cpasterplugin.cpp +++ b/src/plugins/cpaster/cpasterplugin.cpp @@ -90,13 +90,13 @@ void CodePasterService::postText(const QString &text, const QString &mimeType) void CodePasterService::postCurrentEditor() { QTC_ASSERT(CodepasterPlugin::instance(), return); - CodepasterPlugin::instance()->postEditor(); + CodepasterPlugin::instance()->post(CodepasterPlugin::PasteEditor); } void CodePasterService::postClipboard() { QTC_ASSERT(CodepasterPlugin::instance(), return); - CodepasterPlugin::instance()->postClipboard(); + CodepasterPlugin::instance()->post(CodepasterPlugin::PasteClipboard); } // ---------- CodepasterPlugin @@ -104,7 +104,7 @@ CodepasterPlugin *CodepasterPlugin::m_instance = 0; CodepasterPlugin::CodepasterPlugin() : m_settings(new Settings), - m_postEditorAction(0), m_postClipboardAction(0), m_fetchAction(0) + m_postEditorAction(0), m_fetchAction(0) { CodepasterPlugin::m_instance = this; } @@ -165,12 +165,7 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe m_postEditorAction = new QAction(tr("Paste Snippet..."), this); command = Core::ActionManager::registerAction(m_postEditorAction, "CodePaster.Post", globalcontext); command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+C,Meta+P") : tr("Alt+C,Alt+P"))); - connect(m_postEditorAction, SIGNAL(triggered()), this, SLOT(postEditor())); - cpContainer->addAction(command); - - m_postClipboardAction = new QAction(tr("Paste Clipboard..."), this); - command = Core::ActionManager::registerAction(m_postClipboardAction, "CodePaster.PostClipboard", globalcontext); - connect(m_postClipboardAction, SIGNAL(triggered()), this, SLOT(postClipboard())); + connect(m_postEditorAction, &QAction::triggered, this, &CodepasterPlugin::pasteSnippet); cpContainer->addAction(command); m_fetchAction = new QAction(tr("Fetch Snippet..."), this); @@ -204,13 +199,12 @@ ExtensionSystem::IPlugin::ShutdownFlag CodepasterPlugin::aboutToShutdown() return SynchronousShutdown; } -void CodepasterPlugin::postEditor() +static inline void textFromCurrentEditor(QString *text, QString *mimeType) { IEditor *editor = EditorManager::currentEditor(); if (!editor) return; const IDocument *document = editor->document(); - const QString mimeType = document->mimeType(); QString data; if (const BaseTextEditor *textEditor = qobject_cast(editor)) data = textEditor->selectedText(); @@ -223,15 +217,10 @@ void CodepasterPlugin::postEditor() data = textV.toString(); } } - post(data, mimeType); -} - -void CodepasterPlugin::postClipboard() -{ - QString subtype = QLatin1String("plain"); - const QString text = qApp->clipboard()->text(subtype, QClipboard::Clipboard); - if (!text.isEmpty()) - post(text, QString()); + if (!data.isEmpty()) { + *text = data; + *mimeType = document->mimeType(); + } } static inline void fixSpecialCharacters(QString &data) @@ -256,6 +245,19 @@ static inline void fixSpecialCharacters(QString &data) } } +void CodepasterPlugin::post(PasteSources pasteSources) +{ + QString data; + QString mimeType; + if (pasteSources & PasteEditor) + textFromCurrentEditor(&data, &mimeType); + if (data.isEmpty() && (pasteSources & PasteClipboard)) { + QString subType = QStringLiteral("plain"); + data = qApp->clipboard()->text(subType, QClipboard::Clipboard); + } + post(data, mimeType); +} + void CodepasterPlugin::post(QString data, const QString &mimeType) { fixSpecialCharacters(data); @@ -289,6 +291,11 @@ void CodepasterPlugin::fetchUrl() m_urlOpen->fetch(url.toString()); } +void CodepasterPlugin::pasteSnippet() +{ + post(PasteEditor | PasteClipboard); +} + void CodepasterPlugin::fetch() { PasteSelectDialog dialog(m_protocols, ICore::dialogParent()); diff --git a/src/plugins/cpaster/cpasterplugin.h b/src/plugins/cpaster/cpasterplugin.h index d568e2b0461..e9dc9dc9e78 100644 --- a/src/plugins/cpaster/cpasterplugin.h +++ b/src/plugins/cpaster/cpasterplugin.h @@ -64,6 +64,12 @@ class CodepasterPlugin : public ExtensionSystem::IPlugin Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "CodePaster.json") public: + enum PasteSource { + PasteEditor = 0x1, + PasteClipboard = 0x2 + }; + Q_DECLARE_FLAGS(PasteSources, PasteSource) + CodepasterPlugin(); ~CodepasterPlugin(); @@ -74,14 +80,14 @@ public: static CodepasterPlugin *instance(); public slots: - void postEditor(); - void postClipboard(); + void pasteSnippet(); void fetch(); void finishPost(const QString &link); void finishFetch(const QString &titleDescription, const QString &content, bool error); + void post(PasteSources pasteSources); void post(QString data, const QString &mimeType); void fetchUrl(); private: @@ -89,7 +95,6 @@ private: static CodepasterPlugin *m_instance; const QSharedPointer m_settings; QAction *m_postEditorAction; - QAction *m_postClipboardAction; QAction *m_fetchAction; QAction *m_fetchUrlAction; QList m_protocols; @@ -97,6 +102,8 @@ private: Protocol *m_urlOpen; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(CodepasterPlugin::PasteSources) + } // namespace CodePaster #endif // CPASTERPLUGIN_H