• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

Part.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017     02110-1301  USA.
00018 */
00019 
00020 // Own
00021 #include "Part.h"
00022 
00023 // Qt
00024 #include <QtCore/QStringList>
00025 
00026 
00027 // KDE
00028 #include <KAction>
00029 #include <KActionCollection>
00030 #include <KDebug>
00031 #include <KLocale>
00032 #include <KWindowSystem>
00033 #include <kdeversion.h>
00034 
00035 // Konsole
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 // X
00049 #ifdef Q_WS_X11
00050 #include <X11/Xlib.h>
00051 #include <X11/extensions/Xrender.h>
00052 #endif
00053 
00054 extern "C"
00055 {
00056     // entry point for Konsole part library,
00057     // returns a new factory which can be used to construct Konsole parts
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* /*classname*/,
00069                                              const QStringList& /*args*/)
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     // setup global actions
00085     createGlobalActions();
00086 
00087     // create view widget
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     // create basic session
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     // remove existing controller
00227     if (_pluggedController) 
00228     {
00229         removeChildClient (_pluggedController);
00230         disconnect(_pluggedController,SIGNAL(titleChanged(ViewProperties*)),this,
00231                     SLOT(activeViewTitleChanged(ViewProperties*)));
00232     }
00233 
00234     // insert new controller
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     // override all shortcuts in the embedded terminal by default
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     // send a profile change command, the escape code format 
00282     // is the same as the normal X-Term commands used to change the window title or icon,
00283     // but with a magic value of '50' for the parameter which specifies what to change
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"

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal