Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit be120e4e authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Make it possible to copy console's output into the clipboard

parent 0624d21d
......@@ -3,6 +3,7 @@ add_subdirectory(plugins)
qt5_add_resources(KALGEBRAMOBILE_SRCS resources.qrc header.qrc)
add_executable(kalgebramobile pluginsmodel.cpp
../src/consolemodel.cpp
clipboard.cpp
kalgebramobile.cpp main.cpp ${KALGEBRAMOBILE_SRCS})
target_link_libraries(kalgebramobile Qt5::Qml Qt5::Quick Qt5::Gui
......
/*
* Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "clipboard.h"
#include <QGuiApplication>
#include <QMimeData>
#include <QUrl>
#include <QDebug>
Clipboard::Clipboard(QObject* parent)
: QObject(parent)
, m_clipboard(QGuiApplication::clipboard())
, m_mode(QClipboard::Clipboard)
{
connect(m_clipboard, SIGNAL(changed(QClipboard::Mode)), SLOT(clipboardChanged(QClipboard::Mode)));
}
void Clipboard::setMode(QClipboard::Mode mode)
{
m_mode = mode;
emit modeChanged(m_mode);
}
void Clipboard::clipboardChanged(QClipboard::Mode m)
{
if (m == m_mode) {
emit contentChanged();
}
}
void Clipboard::clear()
{
m_clipboard->clear(m_mode);
}
QClipboard::Mode Clipboard::mode() const
{
return m_mode;
}
QVariant Clipboard::contentFormat(const QString &format) const
{
const QMimeData* data = m_clipboard->mimeData(m_mode);
QVariant ret;
if(format == QStringLiteral("text/uri-list")) {
QVariantList retList;
foreach(const QUrl& url, data->urls())
retList += url;
ret = retList;
} else if(format.startsWith(QStringLiteral("text/"))) {
ret = data->text();
} else if(format.startsWith(QStringLiteral("image/"))) {
ret = data->imageData();
} else
ret = data->data(format.isEmpty() ? data->formats().first(): format);
return ret;
}
QVariant Clipboard::content() const
{
return contentFormat(m_clipboard->mimeData(m_mode)->formats().first());
}
void Clipboard::setContent(const QVariant &content)
{
QMimeData* mimeData = new QMimeData;
switch(content.type())
{
case QVariant::String:
mimeData->setText(content.toString());
break;
case QVariant::Color:
mimeData->setColorData(content.toString());
break;
case QVariant::Pixmap:
case QVariant::Image:
mimeData->setImageData(content);
break;
default:
if (content.type() == QVariant::List) {
QVariantList list = content.toList();
QList<QUrl> urls;
bool wasUrlList = true;
foreach (const QVariant& url, list) {
if (url.type() != QVariant::Url) {
wasUrlList = false;
break;
}
urls += url.toUrl();
}
if(wasUrlList) {
mimeData->setUrls(urls);
break;
}
}
if (content.canConvert(QVariant::String)) {
mimeData->setText(content.toString());
} else {
mimeData->setData(QStringLiteral("application/octet-stream"), content.toByteArray());
qWarning() << "Couldn't figure out the content type, storing as application/octet-stream";
}
break;
}
m_clipboard->setMimeData(mimeData, m_mode);
}
QStringList Clipboard::formats() const
{
return m_clipboard->mimeData(m_mode)->formats();
}
/*
* Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef CLIPBOARD_H
#define CLIPBOARD_H
#include <QClipboard>
#include <QVariant>
#include <QMimeData>
class ClipboardPrivate;
class Clipboard : public QObject
{
Q_OBJECT
/**
* Controls the state this object will be monitoring and extracting its contents from.
*/
Q_PROPERTY(QClipboard::Mode mode READ mode WRITE setMode NOTIFY modeChanged)
/**
* Provides the contents currently in the clipboard and lets modify them.
*/
Q_PROPERTY(QVariant content READ content WRITE setContent NOTIFY contentChanged)
/**
* Figure out the nature of the contents in the clipboard.
*/
Q_PROPERTY(QStringList formats READ formats NOTIFY contentChanged)
public:
explicit Clipboard(QObject* parent = nullptr);
QClipboard::Mode mode() const;
void setMode(QClipboard::Mode mode);
Q_SCRIPTABLE QVariant contentFormat(const QString &format) const;
QVariant content() const;
void setContent(const QVariant &content);
QStringList formats() const;
Q_SCRIPTABLE void clear();
Q_SIGNALS:
void modeChanged(QClipboard::Mode mode);
void contentChanged();
private Q_SLOTS:
void clipboardChanged(QClipboard::Mode m);
private:
QClipboard* m_clipboard;
QClipboard::Mode m_mode;
};
#endif
......@@ -27,6 +27,7 @@
#include <QSortFilterProxyModel>
#include <qqml.h>
#include "pluginsmodel.h"
#include "clipboard.h"
using namespace Analitza;
......@@ -41,9 +42,11 @@ KAlgebraMobile::KAlgebraMobile(QObject* parent)
Q_ASSERT(s_self==0);
s_self=this;
const auto uri = "org.kde.kalgebra.mobile";
qmlRegisterType<PluginsModel>("org.kde.kalgebra.mobile", 1, 0, "PluginsModel");
qmlRegisterType<ConsoleModel>("org.kde.kalgebra.mobile", 1, 0, "ConsoleModel");
qmlRegisterType<QSortFilterProxyModel>("org.kde.kalgebra.mobile", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<Clipboard>(uri, 1, 0, "Clipboard");
qmlRegisterType<QAbstractItemModel>();
qmlRegisterUncreatableType<Analitza::Expression>("org.kde.kalgebra.mobile", 1, 0, "Expression", "because");
qRegisterMetaType<QSharedPointer<Analitza::Variables>>("QSharedPointer<Analitza::Variables>");
......
......@@ -17,6 +17,7 @@
*************************************************************************************/
import QtQuick 2.2
import QtQuick.Controls 2.5 as QQC2
import QtQuick.Dialogs 1.0
import org.kde.analitza 1.0
import org.kde.kalgebra.mobile 1.0
......@@ -27,11 +28,28 @@ KAlgebraPage
id: page
ListModel { id: itemModel }
Clipboard {
id: clipboard
}
Component {
id: copyAction
QQC2.Action {
property string valueHtml: "null"
readonly property string value: valueHtml.replace(/<[^>]*>/g, '');
text: i18n("Copy \"%1\"", value)
icon.name: "edit-copy"
onTriggered: {
clipboard.content = value
}
}
}
ConsoleModel {
id: consoleModel
variables: app.variables
onMessage: {
itemModel.append({ result: msg })
itemModel.append({ result: msg, hiddenActions: copyAction.createObject(itemModel, {valueHtml: msg }) })
input.selectAll()
view.currentIndex = view.count-1
view.positionViewAtIndex(view.currentIndex, ListView.Contain)
......
......@@ -37,6 +37,7 @@ ListView
width: scrollList.width - Units.largeSpacing * 4
x: Units.largeSpacing * 2
hiddenActions: model.hiddenActions || []
contentItem: Label { text: model[scrollList.role] }
}
header: scrollList.title ? titleComponent : null
......
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