Commit 7a69585b authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

Codepaster: Join actions, always open dialog.



Remove the "Paste from clipboard" action and move the functionality
into "Paste Snippet". Introduce a new overload for post() taking
a flag mask specifying the sources and use this for the
CodePasterService class (used by the diff editor).

Task-number: QTCREATORBUG-13401
Change-Id: Iadc3560a8a3bdaa817bc4a86007c2682feeb4b77
Reviewed-by: default avatarEike Ziller <eike.ziller@theqtcompany.com>
parent 2d25e629
......@@ -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<const BaseTextEditor *>(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());
......
......@@ -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<Settings> m_settings;
QAction *m_postEditorAction;
QAction *m_postClipboardAction;
QAction *m_fetchAction;
QAction *m_fetchUrlAction;
QList<Protocol*> m_protocols;
......@@ -97,6 +102,8 @@ private:
Protocol *m_urlOpen;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(CodepasterPlugin::PasteSources)
} // namespace CodePaster
#endif // CPASTERPLUGIN_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment