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

Konsole

Part.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2007 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 #if QT_VERSION < KDE_MAKE_VERSION(4,4,0)
00102         action->setShortcutContext(Qt::WidgetShortcut); // remove after Qt4.4 becomes mandatory
00103 #else
00104         action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
00105 #endif
00106 
00107     // create basic session
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     // remove existing controller
00226     if (_pluggedController) 
00227     {
00228         removeChildClient (_pluggedController);
00229         disconnect(_pluggedController,SIGNAL(titleChanged(ViewProperties*)),this,
00230                     SLOT(activeViewTitleChanged(ViewProperties*)));
00231     }
00232 
00233     // insert new controller
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     // send a profile change command, the escape code format 
00269     // is the same as the normal X-Term commands used to change the window title or icon,
00270     // but with a magic value of '50' for the parameter which specifies what to change
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"

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