Actual QML port, moved from QWidget to QQuickPaintedItem. Setup of the project, added sample test-app.

This commit is contained in:
Filippo Scognamiglio
2014-11-06 01:45:17 +01:00
parent 92a5e5d37d
commit e9ee4c9091
13 changed files with 890 additions and 119 deletions
+248
View File
@@ -0,0 +1,248 @@
/*
This file is part of Konsole QML plugin,
which is a terminal emulator from KDE.
Copyright 2013 by Dmitry Zagnoyko <hiroshidi@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "ksession.h"
// Qt
#include <QTextCodec>
// Konsole
#include "KeyboardTranslator.h"
KSession::KSession(QObject *parent) :
QObject(parent), m_session(createSession(""))
{
connect(m_session, SIGNAL(finished()), this, SLOT(sessionFinished()));
connect(m_session, SIGNAL(titleChanged()), this, SIGNAL(titleChanged()));
}
KSession::~KSession()
{
if (m_session) {
m_session->close();
m_session->disconnect();
delete m_session;
}
}
void KSession::setTitle(QString name)
{
m_session->setTitle(Session::NameRole, name);
}
Session *KSession::createSession(QString name)
{
Session *session = new Session();
session->setTitle(Session::NameRole, name);
/* Thats a freaking bad idea!!!!
* /bin/bash is not there on every system
* better set it to the current $SHELL
* Maybe you can also make a list available and then let the widget-owner decide what to use.
* By setting it to $SHELL right away we actually make the first filecheck obsolete.
* But as iam not sure if you want to do anything else ill just let both checks in and set this to $SHELL anyway.
*/
//cool-old-term: There is another check in the code. Not sure if useful.
QString envshell = getenv("SHELL");
QString shellProg = envshell != NULL ? envshell : "/bin/bash";
session->setProgram(shellProg);
setenv("TERM", "xterm", 1);
//session->setProgram();
QStringList args("");
session->setArguments(args);
session->setAutoClose(true);
session->setCodec(QTextCodec::codecForName("UTF-8"));
session->setFlowControlEnabled(true);
session->setHistoryType(HistoryTypeBuffer(1000));
session->setDarkBackground(true);
session->setKeyBindings("");
return session;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
int KSession::getRandomSeed()
{
return m_session->sessionId() * 31;
}
void KSession::addView(TerminalDisplay *display)
{
m_session->addView(display);
}
void KSession::removeView(TerminalDisplay *display)
{
m_session->removeView(display);
}
void KSession::sessionFinished()
{
emit finished();
}
void KSession::selectionChanged(bool textSelected)
{
Q_UNUSED(textSelected)
}
void KSession::startShellProgram()
{
if ( m_session->isRunning() ) {
return;
}
m_session->run();
}
int KSession::getShellPID()
{
return m_session->processId();
}
void KSession::changeDir(const QString &dir)
{
/*
this is a very hackish way of trying to determine if the shell is in
the foreground before attempting to change the directory. It may not
be portable to anything other than Linux.
*/
QString strCmd;
strCmd.setNum(getShellPID());
strCmd.prepend("ps -j ");
strCmd.append(" | tail -1 | awk '{ print $5 }' | grep -q \\+");
int retval = system(strCmd.toStdString().c_str());
if (!retval) {
QString cmd = "cd " + dir + "\n";
sendText(cmd);
}
}
void KSession::setEnvironment(const QStringList &environment)
{
m_session->setEnvironment(environment);
}
void KSession::setShellProgram(const QString &progname)
{
m_session->setProgram(progname);
}
void KSession::setInitialWorkingDirectory(const QString &dir)
{
_initialWorkingDirectory = dir;
m_session->setInitialWorkingDirectory(dir);
}
QString KSession::getInitialWorkingDirectory()
{
return _initialWorkingDirectory;
}
void KSession::setArgs(QStringList &args)
{
m_session->setArguments(args);
}
void KSession::setTextCodec(QTextCodec *codec)
{
m_session->setCodec(codec);
}
void KSession::setHistorySize(int lines)
{
if (lines < 0)
m_session->setHistoryType(HistoryTypeFile());
else
m_session->setHistoryType(HistoryTypeBuffer(lines));
}
void KSession::sendText(QString text)
{
m_session->sendText(text);
}
void KSession::sendKey(int rep, int key, int mod) const
{
//TODO implement or remove this function.
// Qt::KeyboardModifier kbm = Qt::KeyboardModifier(mod);
// QKeyEvent qkey(QEvent::KeyPress, key, kbm);
// while (rep > 0){
// m_session->sendKey(&qkey);
// --rep;
// }
}
void KSession::setFlowControlEnabled(bool enabled)
{
m_session->setFlowControlEnabled(enabled);
}
bool KSession::flowControlEnabled()
{
return m_session->flowControlEnabled();
}
void KSession::setKeyBindings(const QString &kb)
{
m_session->setKeyBindings(kb);
emit changedKeyBindings(kb);
}
QString KSession::getKeyBindings()
{
return m_session->keyBindings();
}
QStringList KSession::availableKeyBindings()
{
return KeyboardTranslatorManager::instance()->allTranslators();
}
QString KSession::keyBindings()
{
return m_session->keyBindings();
}
QString KSession::getTitle()
{
return m_session->userTitle();
}
+142
View File
@@ -0,0 +1,142 @@
/*
This file is part of Konsole QML plugin,
which is a terminal emulator from KDE.
Copyright 2013 by Dmitry Zagnoyko <hiroshidi@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#ifndef KSESSION_H
#define KSESSION_H
#include <QObject>
// Konsole
#include "Session.h"
//#include "TerminalDisplay.h"
using namespace Konsole;
class KSession : public QObject
{
Q_OBJECT
Q_PROPERTY(QString kbScheme READ getKeyBindings WRITE setKeyBindings NOTIFY changedKeyBindings)
Q_PROPERTY(QString initialWorkingDirectory READ getInitialWorkingDirectory WRITE setInitialWorkingDirectory)
Q_PROPERTY(QString title READ getTitle NOTIFY titleChanged)
Q_PROPERTY(QString shellProgram WRITE setShellProgram)
public:
KSession(QObject *parent = 0);
~KSession();
public:
//bool setup();
void addView(TerminalDisplay *display);
void removeView(TerminalDisplay *display);
int getRandomSeed();
QString getKeyBindings();
//look-n-feel, if you don`t like defaults
//environment
void setEnvironment(const QStringList & environment);
//Initial working directory
void setInitialWorkingDirectory(const QString & dir);
QString getInitialWorkingDirectory();
// Shell program args, default is none
void setArgs(QStringList & args);
//Text codec, default is UTF-8
void setTextCodec(QTextCodec * codec);
// History size for scrolling
void setHistorySize(int lines); //infinite if lines < 0
// Sets whether flow control is enabled
void setFlowControlEnabled(bool enabled);
// Returns whether flow control is enabled
bool flowControlEnabled(void);
/**
* Sets whether the flow control warning box should be shown
* when the flow control stop key (Ctrl+S) is pressed.
*/
//void setFlowControlWarningEnabled(bool enabled);
/*! Get all available keyboard bindings
*/
static QStringList availableKeyBindings();
//! Return current key bindings
QString keyBindings();
QString getTitle();
signals:
void finished();
void copyAvailable(bool);
void termGetFocus();
void termLostFocus();
void termKeyPressed(QKeyEvent *);
void changedKeyBindings(QString kb);
void titleChanged();
public slots:
/*! Set named key binding for given widget
*/
void setKeyBindings(const QString & kb);
void setTitle(QString name);
void startShellProgram();
// Shell program, default is /bin/bash
void setShellProgram(const QString & progname);
int getShellPID();
void changeDir(const QString & dir);
// Send some text to terminal
void sendText(QString text);
// Send some text to terminal
void sendKey(int rep, int key, int mod) const;
protected slots:
void sessionFinished();
void selectionChanged(bool textSelected);
private slots:
Session* createSession(QString name);
//Konsole::KTerminalDisplay* createTerminalDisplay(Konsole::Session *session, QQuickItem* parent);
private:
//Konsole::KTerminalDisplay *m_terminalDisplay;
QString _initialWorkingDirectory;
Session *m_session;
};
#endif // KSESSION_H
+3
View File
@@ -0,0 +1,3 @@
module QMLTermWidget
plugin qmltermwidget
+38
View File
@@ -0,0 +1,38 @@
#include "qmltermwidget_plugin.h"
#include "TerminalDisplay.h"
#include "ksession.h"
#include <qqml.h>
#include <QQmlEngine>
#include <QDir>
using namespace Konsole;
void QmltermwidgetPlugin::registerTypes(const char *uri)
{
// @uri org.qterminal.qmlterminal
qmlRegisterType<TerminalDisplay>(uri, 1, 0, "QMLTermWidget");
qmlRegisterType<KSession>(uri, 1, 0, "QMLTermSession");
}
void QmltermwidgetPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
{
QQmlExtensionPlugin::initializeEngine(engine, uri);
QStringList pwds = engine->importPathList();
if (!pwds.empty()){
QString cs, kbl;
foreach (QString pwd, pwds) {
cs = pwd + "/QMLTermWidget/color-schemes";
kbl = pwd + "/QMLTermWidget/kb-layouts";
if (QDir(cs).exists()) break;
}
setenv("KB_LAYOUT_DIR",kbl.toUtf8().constData(),1);
setenv("COLORSCHEMES_DIR",cs.toUtf8().constData(),1);
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef QMLTERMWIDGET_PLUGIN_H
#define QMLTERMWIDGET_PLUGIN_H
#include <QQmlExtensionPlugin>
class QmltermwidgetPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qterminal.qmlterminal.QMLTermWidget")
public:
void registerTypes(const char *uri);
void initializeEngine(QQmlEngine *engine, const char *uri);
};
#endif // QMLTERMWIDGET_PLUGIN_H