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 action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
00102
00103
00104 createSession();
00105 }
00106 Part::~Part()
00107 {
00108 SessionManager::instance()->saveState();
00109 }
00110 void Part::createGlobalActions()
00111 {
00112 _manageProfilesAction = new QAction(i18n("Manage Profiles..."),this);
00113 connect(_manageProfilesAction,SIGNAL(triggered()),this,SLOT(showManageProfilesDialog()));
00114 }
00115 void Part::setupActionsForSession(SessionController* session)
00116 {
00117 KActionCollection* collection = session->actionCollection();
00118 collection->addAction("manage-profiles",_manageProfilesAction);
00119 }
00120 bool Part::transparencyAvailable()
00121 {
00122 #ifdef Q_WS_X11
00123 bool ARGB = false;
00124
00125 int screen = QX11Info::appScreen();
00126 bool depth = (QX11Info::appDepth() == 32);
00127
00128 Display* display = QX11Info::display();
00129 Visual* visual = static_cast<Visual*>(QX11Info::appVisual(screen));
00130
00131 XRenderPictFormat* format = XRenderFindVisualFormat(display, visual);
00132
00133 if (depth && format->type == PictTypeDirect && format->direct.alphaMask)
00134 {
00135 ARGB = true;
00136 }
00137
00138 if (ARGB)
00139 {
00140 return KWindowSystem::compositingActive();
00141 }
00142 else
00143 #endif
00144 {
00145 return false;
00146 }
00147 }
00148
00149 bool Part::openFile()
00150 {
00151 return false;
00152 }
00153 void Part::terminalExited()
00154 {
00155 deleteLater();
00156 }
00157 void Part::newTab()
00158 {
00159 createSession();
00160 showShellInDir( QString() );
00161 }
00162 Session* Part::activeSession() const
00163 {
00164 if ( _viewManager->activeViewController() )
00165 {
00166 Q_ASSERT( _viewManager->activeViewController()->session());
00167
00168 return _viewManager->activeViewController()->session();
00169 }
00170 else
00171 {
00172 return 0;
00173 }
00174 }
00175 void Part::startProgram( const QString& program,
00176 const QStringList& arguments )
00177 {
00178 Q_ASSERT( activeSession() );
00179
00180 if ( !activeSession()->isRunning() )
00181 {
00182 if ( !program.isEmpty() && !arguments.isEmpty() )
00183 {
00184 activeSession()->setProgram(program);
00185 activeSession()->setArguments(arguments);
00186 }
00187
00188 activeSession()->run();
00189 }
00190 }
00191 void Part::openTeletype(int fd)
00192 {
00193 Q_ASSERT( activeSession() );
00194
00195 activeSession()->openTeletype(fd);
00196 }
00197 void Part::showShellInDir( const QString& dir )
00198 {
00199 Q_ASSERT( activeSession() );
00200
00201 if ( !activeSession()->isRunning() )
00202 {
00203 if ( !dir.isEmpty() )
00204 activeSession()->setInitialWorkingDirectory(dir);
00205 activeSession()->run();
00206 }
00207 }
00208 void Part::sendInput( const QString& text )
00209 {
00210 Q_ASSERT( activeSession() );
00211 activeSession()->emulation()->sendText(text);
00212 }
00213
00214 Session* Part::createSession(const Profile::Ptr profile)
00215 {
00216 Session* session = SessionManager::instance()->createSession(profile);
00217 _viewManager->createView(session);
00218
00219 return session;
00220 }
00221 void Part::activeViewChanged(SessionController* controller)
00222 {
00223 Q_ASSERT( controller );
00224 Q_ASSERT( controller->view() );
00225
00226
00227 if (_pluggedController)
00228 {
00229 removeChildClient (_pluggedController);
00230 disconnect(_pluggedController,SIGNAL(titleChanged(ViewProperties*)),this,
00231 SLOT(activeViewTitleChanged(ViewProperties*)));
00232 }
00233
00234
00235 setupActionsForSession(controller);
00236 insertChildClient(controller);
00237 connect(controller,SIGNAL(titleChanged(ViewProperties*)),this,
00238 SLOT(activeViewTitleChanged(ViewProperties*)));
00239 activeViewTitleChanged(controller);
00240
00241 const char* displaySignal = SIGNAL(overrideShortcutCheck(QKeyEvent*,bool&));
00242 const char* partSlot = SLOT(overrideTerminalShortcut(QKeyEvent*,bool&));
00243
00244 disconnect(controller->view(),displaySignal,this,partSlot);
00245 connect(controller->view(),displaySignal,this,partSlot);
00246
00247 _pluggedController = controller;
00248 }
00249 void Part::overrideTerminalShortcut(QKeyEvent* event, bool& override)
00250 {
00251
00252 override = true;
00253 emit overrideShortcut(event,override);
00254 }
00255 void Part::activeViewTitleChanged(ViewProperties* properties)
00256 {
00257 emit setWindowCaption(properties->title());
00258 }
00259 void Part::showManageProfilesDialog()
00260 {
00261 showManageProfilesDialog(_viewManager->widget());
00262 }
00263 void Part::showManageProfilesDialog(QWidget* parent)
00264 {
00265 ManageProfilesDialog* dialog = new ManageProfilesDialog(parent);
00266 dialog->setAttribute(Qt::WA_DeleteOnClose);
00267 dialog->setShortcutEditorVisible(false);
00268 dialog->show();
00269 }
00270 void Part::showEditCurrentProfileDialog(QWidget* parent)
00271 {
00272 Q_ASSERT( activeSession() );
00273
00274 EditProfileDialog* dialog = new EditProfileDialog(parent);
00275 dialog->setAttribute(Qt::WA_DeleteOnClose);
00276 dialog->setProfile( SessionManager::instance()->sessionProfile(activeSession()) );
00277 dialog->show();
00278 }
00279 void Part::changeSessionSettings(const QString& text)
00280 {
00281
00282
00283
00284 Q_ASSERT( activeSession() );
00285 QByteArray buffer;
00286 buffer.append("\033]50;").append(text.toUtf8()).append('\a');
00287
00288 activeSession()->emulation()->receiveData(buffer.constData(),buffer.length());
00289 }
00290
00291 #include "Part.moc"