1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/***************************************************************************
* Copyright 2003, 2006 Adam Treat <treat@kde.org> *
* Copyright 2007 Andreas Pakulat <apaku@gmx.de> *
* *
* 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. *
* *
***************************************************************************/
#include "kdevkonsoleview.h"
#include "debug.h"
#include <QFrame>
#include <QIcon>
#include <QKeyEvent>
#include <QUuid>
#include <QVBoxLayout>
#include <kde_terminal_interface.h>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KParts/ReadOnlyPart>
#include <interfaces/icore.h>
#include <interfaces/isession.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <util/path.h>
#include "kdevkonsoleviewplugin.h"
class KDevKonsoleViewPrivate
{
public:
KDevKonsoleViewPlugin* mplugin;
KDevKonsoleView* m_view;
KParts::ReadOnlyPart *konsolepart;
QVBoxLayout *m_vbox;
// TODO: remove this once we can depend on a Qt version that includes https://codereview.qt-project.org/#/c/83800/
QMetaObject::Connection m_partDestroyedConnection;
void _k_slotTerminalClosed();
void init( KPluginFactory* factory )
{
Q_ASSERT( konsolepart == nullptr );
Q_ASSERT( factory != nullptr );
if ( ( konsolepart = factory->create<KParts::ReadOnlyPart>( m_view ) ) )
{
QObject::disconnect(m_partDestroyedConnection);
m_partDestroyedConnection = QObject::connect(konsolepart, &KParts::ReadOnlyPart::destroyed,
m_view, [&] { _k_slotTerminalClosed(); });
konsolepart->widget() ->setFocusPolicy( Qt::WheelFocus );
konsolepart->widget() ->setFocus();
konsolepart->widget() ->installEventFilter( m_view );
if ( auto * frame = qobject_cast<QFrame*>( konsolepart->widget() ) )
frame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
m_vbox->addWidget( konsolepart->widget() );
m_view->setFocusProxy( konsolepart->widget() );
konsolepart->widget() ->show();
TerminalInterface* interface = qobject_cast<TerminalInterface*>(konsolepart);
Q_ASSERT(interface);
QString dir;
if (KDevelop::IDocument* activeDocument = KDevelop::ICore::self()->documentController()->activeDocument())
{
KDevelop::IProject* project = KDevelop::ICore::self()->projectController()->findProjectForUrl(activeDocument->url());
if (project && project->path().isLocalFile())
dir = project->path().path();
else if (activeDocument->url().isLocalFile())
dir = activeDocument->url().adjusted(QUrl::RemoveFilename).path(QUrl::FullyDecoded);
}
interface->showShellInDir( dir );
interface->sendInput(QLatin1String(" kdevelop! -s \"") + KDevelop::ICore::self()->activeSession()->id().toString() + QLatin1String("\"\n"));
}else
{
qCDebug(PLUGIN_KONSOLE) << "Couldn't create KParts::ReadOnlyPart from konsole factory!";
}
}
~KDevKonsoleViewPrivate()
{
QObject::disconnect(m_partDestroyedConnection);
}
};
void KDevKonsoleViewPrivate::_k_slotTerminalClosed()
{
konsolepart = nullptr;
init( mplugin->konsoleFactory() );
}
KDevKonsoleView::KDevKonsoleView( KDevKonsoleViewPlugin *plugin, QWidget* parent )
: QWidget( parent ), d(new KDevKonsoleViewPrivate)
{
d->mplugin = plugin;
d->m_view = this;
d->konsolepart = nullptr;
setObjectName( i18n( "Konsole" ) );
setWindowIcon( QIcon::fromTheme( QStringLiteral( "utilities-terminal" ), windowIcon() ) );
setWindowTitle(i18nc("@title:window", "Konsole"));
d->m_vbox = new QVBoxLayout( this );
d->m_vbox->setContentsMargins(0, 0, 0, 0);
d->m_vbox->setSpacing( 0 );
d->init( d->mplugin->konsoleFactory() );
//TODO Make this configurable in the future,
// but by default the konsole shouldn't
// automatically switch directories on you.
// connect( KDevelop::Core::documentController(), SIGNAL(documentActivated(KDevDocument*)),
// this, SLOT(documentActivated(KDevDocument*)) );
}
KDevKonsoleView::~KDevKonsoleView()
{
delete d;
}
void KDevKonsoleView::setDirectory( const QUrl &url )
{
if ( !url.isValid() || !url.isLocalFile() )
return ;
if ( d->konsolepart && url != d->konsolepart->url() )
d->konsolepart->openUrl( url );
}
bool KDevKonsoleView::eventFilter( QObject* obj, QEvent *e )
{
switch( e->type() ) {
case QEvent::ShortcutOverride: {
auto *k = static_cast<QKeyEvent *>(e);
// Don't propagate Esc to the top level, it should be used by konsole
if (k->key() == Qt::Key_Escape) {
if (d->konsolepart && d->konsolepart->widget()) {
e->accept();
return true;
}
}
break;
}
default:
break;
}
return QWidget::eventFilter( obj, e );
}
#include "moc_kdevkonsoleview.cpp"
|