Konsole
Part.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "Part.h"
00022
00023
00024 #include <QtCore/QStringList>
00025
00026
00027
00028 #include <KAction>
00029 #include <KActionCollection>
00030 #include <KDebug>
00031 #include <KLocale>
00032 #include <KWindowSystem>
00033 #include <kdeversion.h>
00034
00035
00036 #include "ColorScheme.h"
00037 #include "EditProfileDialog.h"
00038 #include "Emulation.h"
00039 #include "KeyboardTranslator.h"
00040 #include "ManageProfilesDialog.h"
00041 #include "Session.h"
00042 #include "SessionController.h"
00043 #include "SessionManager.h"
00044 #include "TerminalDisplay.h"
00045 #include "ViewManager.h"
00046 #include "MainWindow.h"
00047
00048
00049 #ifdef Q_WS_X11
00050 #include <X11/Xlib.h>
00051 #include <X11/extensions/Xrender.h>
00052 #endif
00053
00054 extern "C"
00055 {
00056
00057
00058 KDE_EXPORT void* init_libkonsolepart()
00059 {
00060 return new Konsole::PartFactory;
00061 }
00062 }
00063
00064 using namespace Konsole;
00065
00066 KParts::Part* PartFactory::createPartObject( QWidget* parentWidget,
00067 QObject* parent,
00068 const char* ,
00069 const QStringList& )
00070 {
00071 return new Part(parentWidget,parent);
00072 }
00073
00074 K_EXPORT_PLUGIN(Konsole::PartFactory())
00075
00076 Part::Part(QWidget* parentWidget , QObject* parent)
00077 : KParts::ReadOnlyPart(parent)
00078 ,_viewManager(0)
00079 ,_pluggedController(0)
00080 ,_manageProfilesAction(0)
00081 {
00082 TerminalDisplay::HAVE_TRANSPARENCY = transparencyAvailable();
00083
00084
00085 createGlobalActions();
00086
00087
00088 _viewManager = new ViewManager(this,actionCollection());
00089 _viewManager->setNavigationMethod( ViewManager::NoNavigation );
00090
00091 connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
00092 SLOT(activeViewChanged(SessionController*)) );
00093 connect( _viewManager , SIGNAL(empty()) , this , SLOT(terminalExited()) );
00094 connect( _viewManager , SIGNAL(newViewRequest()) , this , SLOT(newTab()) );
00095
00096 _viewManager->widget()->setParent(parentWidget);
00097
00098 setWidget(_viewManager->widget());
00099 actionCollection()->addAssociatedWidget(_viewManager->widget());
00100 foreach (QAction* action, actionCollection()->actions())
00101 #if QT_VERSION < KDE_MAKE_VERSION(4,4,0)
00102 action->setShortcutContext(Qt::WidgetShortcut);
00103 #else
00104 action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
00105 #endif
00106
00107
00108 createSession(QString());
00109 }
00110 Part::~Part()
00111 {
00112 }
00113 void Part::createGlobalActions()
00114 {
00115 _manageProfilesAction = new QAction(i18n("Manage Profiles..."),this);
00116 connect(_manageProfilesAction,SIGNAL(triggered()),this,SLOT(showManageProfilesDialog()));
00117 }
00118 void Part::setupActionsForSession(SessionController* session)
00119 {
00120 KActionCollection* collection = session->actionCollection();
00121 collection->addAction("manage-profiles",_manageProfilesAction);
00122 }
00123 bool Part::transparencyAvailable()
00124 {
00125 #ifdef Q_WS_X11
00126 bool ARGB = false;
00127
00128 int screen = QX11Info::appScreen();
00129 bool depth = (QX11Info::appDepth() == 32);
00130
00131 Display* display = QX11Info::display();
00132 Visual* visual = static_cast<Visual*>(QX11Info::appVisual(screen));
00133
00134 XRenderPictFormat* format = XRenderFindVisualFormat(display, visual);
00135
00136 if (depth && format->type == PictTypeDirect && format->direct.alphaMask)
00137 {
00138 ARGB = true;
00139 }
00140
00141 if (ARGB)
00142 {
00143 return KWindowSystem::compositingActive();
00144 }
00145 else
00146 #endif
00147 {
00148 return false;
00149 }
00150 }
00151
00152 bool Part::openFile()
00153 {
00154 return false;
00155 }
00156 void Part::terminalExited()
00157 {
00158 deleteLater();
00159 }
00160 void Part::newTab()
00161 {
00162 createSession( QString() );
00163 showShellInDir( QString() );
00164 }
00165 Session* Part::activeSession() const
00166 {
00167 if ( _viewManager->activeViewController() )
00168 {
00169 Q_ASSERT( _viewManager->activeViewController()->session());
00170
00171 return _viewManager->activeViewController()->session();
00172 }
00173 else
00174 {
00175 return 0;
00176 }
00177 }
00178 void Part::startProgram( const QString& program,
00179 const QStringList& arguments )
00180 {
00181 Q_ASSERT( activeSession() );
00182
00183 if ( !activeSession()->isRunning() )
00184 {
00185 if ( !program.isEmpty() && !arguments.isEmpty() )
00186 {
00187 activeSession()->setProgram(program);
00188 activeSession()->setArguments(arguments);
00189 }
00190
00191 activeSession()->run();
00192 }
00193 }
00194 void Part::showShellInDir( const QString& dir )
00195 {
00196 Q_ASSERT( activeSession() );
00197
00198 if ( !activeSession()->isRunning() )
00199 {
00200 if ( !dir.isEmpty() )
00201 activeSession()->setInitialWorkingDirectory(dir);
00202 activeSession()->run();
00203 }
00204 }
00205 void Part::sendInput( const QString& text )
00206 {
00207 Q_ASSERT( activeSession() );
00208 activeSession()->emulation()->sendText(text);
00209 }
00210
00211 Session* Part::createSession(const QString& key)
00212 {
00213 Session* session = SessionManager::instance()->createSession(key);
00214 _viewManager->createView(session);
00215
00216 return session;
00217 }
00218 void Part::activeViewChanged(SessionController* controller)
00219 {
00220 Q_ASSERT( controller );
00221 Q_ASSERT( controller->view() );
00222
00223 widget()->setFocusProxy( controller->view() );
00224
00225
00226 if (_pluggedController)
00227 {
00228 removeChildClient (_pluggedController);
00229 disconnect(_pluggedController,SIGNAL(titleChanged(ViewProperties*)),this,
00230 SLOT(activeViewTitleChanged(ViewProperties*)));
00231 }
00232
00233
00234 setupActionsForSession(controller);
00235 insertChildClient(controller);
00236 connect(controller,SIGNAL(titleChanged(ViewProperties*)),this,
00237 SLOT(activeViewTitleChanged(ViewProperties*)));
00238 activeViewTitleChanged(controller);
00239
00240 _pluggedController = controller;
00241 }
00242 void Part::activeViewTitleChanged(ViewProperties* properties)
00243 {
00244 emit setWindowCaption(properties->title());
00245 }
00246 void Part::showManageProfilesDialog()
00247 {
00248 showManageProfilesDialog(_viewManager->widget());
00249 }
00250 void Part::showManageProfilesDialog(QWidget* parent)
00251 {
00252 ManageProfilesDialog* dialog = new ManageProfilesDialog(parent);
00253 dialog->setAttribute(Qt::WA_DeleteOnClose);
00254 dialog->setShortcutEditorVisible(false);
00255 dialog->show();
00256 }
00257 void Part::showEditCurrentProfileDialog(QWidget* parent)
00258 {
00259 Q_ASSERT( activeSession() );
00260
00261 EditProfileDialog* dialog = new EditProfileDialog(parent);
00262 dialog->setAttribute(Qt::WA_DeleteOnClose);
00263 dialog->setProfile( activeSession()->profileKey() );
00264 dialog->show();
00265 }
00266 void Part::changeSessionSettings(const QString& text)
00267 {
00268
00269
00270
00271 Q_ASSERT( activeSession() );
00272 QByteArray buffer;
00273 buffer.append("\033]50;").append(text.toUtf8()).append('\a');
00274
00275 activeSession()->emulation()->receiveData(buffer.constData(),buffer.length());
00276 }
00277
00278 #include "Part.moc"