MainWindow.cpp 10.3 KB
Newer Older
1
/*
2
    Copyright (C) 2006-2007 by Robert Knight <robertknight@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20 21 22
// Own
#include "MainWindow.h"

23
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24
#include <QtGui/QBoxLayout>
25

26 27 28 29
// KDE
#include <KAction>
#include <KActionCollection>
#include <KActionMenu>
30
#include <KShortcutsDialog>
31 32
#include <KLocale>
#include <KMenu>
33
#include <KMenuBar>
34
#include <KService>
35
#include <KToggleAction>
36
#include <KToggleFullScreenAction>
37
#include <KToolInvocation>
38
#include <kstandardaction.h>
39
#include <KXMLGUIFactory>
40 41

// Konsole
42
#include "BookmarkHandler.h"
43 44
#include "IncrementalSearchBar.h"
#include "RemoteConnectionDialog.h"
45
#include "SessionController.h"
46 47
#include "ProfileList.h"
#include "ManageProfilesDialog.h"
48 49
#include "ViewManager.h"
#include "ViewSplitter.h"
50

51 52
using namespace Konsole;

53
MainWindow::MainWindow()
54
 : KXmlGuiWindow() ,
55 56
   _bookmarkHandler(0),
   _pluggedController(0)
57
{
58
    // create actions for menus
59 60
    // the directory ('konsole') is included in the path here so that the XML
    // file can be found when this code is being used in the Konsole part.
61
    setXMLFile("konsole/konsoleui.rc");
62 63 64
    setupActions();

    // create view manager
65
        _viewManager = new ViewManager(this,actionCollection());
Robert Knight's avatar
 
Robert Knight committed
66
    connect( _viewManager , SIGNAL(empty()) , this , SLOT(close()) );
67 68 69 70
    connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
            SLOT(activeViewChanged(SessionController*)) );
    connect( _viewManager , SIGNAL(viewPropertiesChanged(const QList<ViewProperties*>&)) ,
           bookmarkHandler() , SLOT(setViews(const QList<ViewProperties*>&)) ); 
71

72 73 74
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

75 76 77
    // create main window widgets
    setupWidgets();

78 79
    // create menus
    createGUI();
80 81 82 83

    // replace standard shortcuts which cannot be used in a terminal
    // (as they are reserved for use by terminal programs)
    correctShortcuts();
84 85 86 87 88 89 90

    // add all actions to this window, so that the keyboard shortcuts can be
    // used even when the menu is hidden
    //
    // See comments in SessionController constructor about Qt 4 behaviour with
    // hidden menu bars and shortcuts
    addActions(actionCollection()->actions());
91 92 93 94 95 96 97 98 99 100
}

void MainWindow::correctShortcuts()
{
    // replace F1 shortcut for help contents
    QAction* helpAction = actionCollection()->action("help_contents");
    
    Q_ASSERT( helpAction );

    helpAction->setShortcut( QKeySequence() );
101 102
}

103 104 105 106 107 108 109 110 111
void MainWindow::setDefaultProfile(const QString& key)
{
    _defaultProfile = key;
}
QString MainWindow::defaultProfile() const
{
    return _defaultProfile;
}

112
ViewManager* MainWindow::viewManager() const
113 114 115 116
{
    return _viewManager;
}

117 118 119 120 121
void MainWindow::activeViewChanged(SessionController* controller)
{
    if ( _pluggedController == controller )
        return;

122
    // associate bookmark menu with current session
123 124 125 126 127 128 129
    bookmarkHandler()->setActiveView(controller);
    disconnect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , 0 , 0 );
    connect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , controller ,
             SLOT(openUrl(const KUrl&)) );

    // set the current session's search bar
    controller->setSearchBar( searchBar() );
130
    controller->setShowMenuAction( _toggleMenuBarAction );
131 132 133 134 135 136

    // listen for title changes from the current session
    if ( _pluggedController )
    {
        disconnect( _pluggedController , SIGNAL(titleChanged(ViewProperties*)) 
                     , this , SLOT(activeViewTitleChanged(ViewProperties*)) );
137
        guiFactory()->removeClient(_pluggedController);
138 139
    }

140 141
    Q_ASSERT( controller );

142 143 144 145 146 147 148 149 150 151 152 153 154 155
    connect( controller , SIGNAL(titleChanged(ViewProperties*)) , 
            this , SLOT(activeViewTitleChanged(ViewProperties*)) );

    guiFactory()->addClient(controller);

    // update session title to match newly activated session
    activeViewTitleChanged(controller);

    _pluggedController = controller;
}

void MainWindow::activeViewTitleChanged(ViewProperties* properties)
{
    setPlainCaption(properties->title());
156
    setWindowIcon(properties->icon());
157 158
}

159
IncrementalSearchBar* MainWindow::searchBar() const
160 161 162 163
{
    return _searchBar;
}

164
void MainWindow::setupActions()
165
{
166 167
    KActionCollection* collection = actionCollection();

168
    // File Menu
169 170 171
    QAction* newTabAction = collection->addAction("new-tab"); 
    newTabAction->setIcon( KIcon("openterm") );
    newTabAction->setText( i18n("New &Tab") );
Robert Knight's avatar
 
Robert Knight committed
172
    newTabAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_N) );
173
    connect( newTabAction , SIGNAL(triggered()) , this , SLOT(newTab()) );
174 175

    QAction* newWindowAction = collection->addAction("new-window"); 
Robert Knight's avatar
Robert Knight committed
176
    newWindowAction->setIcon( KIcon("window-new") );
177
    newWindowAction->setText( i18n("New &Window") );
Robert Knight's avatar
 
Robert Knight committed
178
    newWindowAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_M) );
179
    connect( newWindowAction , SIGNAL(triggered()) , this , SLOT(newWindow()) );
180

181 182 183 184 185 186
    QAction* remoteConnectionAction = collection->addAction("remote-connection");
    remoteConnectionAction->setText( i18n("Remote Connection...") );
    remoteConnectionAction->setIcon( KIcon("network") );
    remoteConnectionAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_R) );
    connect( remoteConnectionAction , SIGNAL(triggered()) , this , SLOT(showRemoteConnectionDialog()) ); 

187
       
188
#ifndef KONSOLE_PART 
189 190 191 192
    QAction* quitAction = KStandardAction::quit( this , SLOT(close()) , collection );
    // the default shortcut for quit is typically Ctrl+[Some Letter, usually Q] but that is reserved for 
    // use by terminal applications
    quitAction->setShortcut(Qt::CTRL+Qt::SHIFT+Qt::Key_Q);
193
#endif
194 195

    // Bookmark Menu
196
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
197
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
198
    collection->addAction("bookmark" , bookmarkMenu);
199

200 201 202
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

203
    // View Menu
204 205 206
    _toggleMenuBarAction = new KToggleAction(this);
    _toggleMenuBarAction->setText( i18n("Show Menu Bar") );
    _toggleMenuBarAction->setIcon( KIcon("show-menu") );
207
    _toggleMenuBarAction->setCheckedState( KGuiItem( i18n("Hide Menu Bar") , "show-menu") ); 
208 209 210
    _toggleMenuBarAction->setChecked( !menuBar()->isHidden() );
    connect( _toggleMenuBarAction , SIGNAL(toggled(bool)) , menuBar() , SLOT(setVisible(bool)) );
    collection->addAction("show-menubar",_toggleMenuBarAction);
211

212 213
    KToggleFullScreenAction* fullScreenAction = new KToggleFullScreenAction(this);
    fullScreenAction->setWindow(this);
214
    fullScreenAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_F11 );
215 216
    collection->addAction("view-full-screen",fullScreenAction);
    connect( fullScreenAction , SIGNAL(toggled(bool)) , this , SLOT(viewFullScreen(bool)) ); 
217 218 219 220 221
    //TODO - Implmement this correctly
    //
    //QAction* mergeAction = collection->addAction("merge-windows");
    //mergeAction->setText( i18n("&Merge Windows") );
    //connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );
222

223
    // Settings Menu
224 225
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
226 227 228

    QAction* manageProfilesAction = collection->addAction("manage-profiles");
    manageProfilesAction->setText( i18n("Manage Profiles...") );
229
    manageProfilesAction->setIcon( KIcon("configure") );
230 231
    connect( manageProfilesAction , SIGNAL(triggered()) , this , SLOT(showManageProfilesDialog()) );

232 233
}

234 235 236 237 238 239 240
void MainWindow::viewFullScreen(bool fullScreen) 
{
    if ( fullScreen )
        showFullScreen();
    else
        showNormal();
}
241
BookmarkHandler* MainWindow::bookmarkHandler() const
242 243 244 245
{
    return _bookmarkHandler;
}

246
void MainWindow::setSessionList(ProfileList* list)
247
{
248
    sessionListChanged(list->actions());
249

250 251
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
252 253 254 255 256 257 258

    connect( list , SIGNAL(actionsChanged(const QList<QAction*>&)) , this ,
            SLOT(sessionListChanged(const QList<QAction*>&)) );
}

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
259 260
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
261 262
}

263 264 265 266 267 268 269 270
QString MainWindow::activeSessionDir() const
{
    if ( _pluggedController )
        return _pluggedController->currentDir();
    else
        return QString();
}

271
void MainWindow::newTab()
272
{
273
    emit newSessionRequest( _defaultProfile , activeSessionDir() , _viewManager);
274 275
}

276
void MainWindow::newWindow()
277
{
278
    emit newWindowRequest( _defaultProfile , activeSessionDir() );
279 280
}

281
void MainWindow::showShortcutsDialog()
282
{
283 284
    KShortcutsDialog::configure( actionCollection() ,
                                 KShortcutsEditor::LetterShortcutsDisallowed );
285 286
}

287
void MainWindow::newFromProfile(const QString& key)
288
{
289
        emit newSessionRequest(key,QString(),_viewManager);
290
}
291
void MainWindow::showManageProfilesDialog()
292
{
293 294
    ManageProfilesDialog* dialog = new ManageProfilesDialog(this);
    dialog->show();
295
}
296

297 298 299 300
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
301
        emit newSessionRequest(dialog.sessionKey(),QString(),_viewManager);
302 303
}

304
void MainWindow::setupWidgets()
305
{
306 307 308 309 310
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

    _searchBar = new IncrementalSearchBar( IncrementalSearchBar::AllFeatures , this);
    _searchBar->setVisible(false);
311

312 313 314 315
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
316

317 318 319
    widget->setLayout(layout);
    
    setCentralWidget(widget);
320
}
321

322
#include "MainWindow.moc"