MainWindow.cpp 10.1 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

    actionCollection()->setAssociatedWidget(this);
64 65 66
    setupActions();

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

74 75 76
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

77 78 79
    // create main window widgets
    setupWidgets();

80 81
    // create menus
    createGUI();
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    // replace standard shortcuts which cannot be used in a terminal
    // (as they are reserved for use by terminal programs)
    correctShortcuts();
}

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

    helpAction->setShortcut( QKeySequence() );
96 97
}

98 99 100 101 102 103 104 105 106
void MainWindow::setDefaultProfile(const QString& key)
{
    _defaultProfile = key;
}
QString MainWindow::defaultProfile() const
{
    return _defaultProfile;
}

107
ViewManager* MainWindow::viewManager() const
108 109 110 111
{
    return _viewManager;
}

112 113 114 115 116
void MainWindow::activeViewChanged(SessionController* controller)
{
    if ( _pluggedController == controller )
        return;

117
    // associate bookmark menu with current session
118 119 120 121 122
    bookmarkHandler()->setActiveView(controller);
    disconnect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , 0 , 0 );
    connect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , controller ,
             SLOT(openUrl(const KUrl&)) );

123
 
124 125 126 127 128
    // listen for title changes from the current session
    if ( _pluggedController )
    {
        disconnect( _pluggedController , SIGNAL(titleChanged(ViewProperties*)) 
                     , this , SLOT(activeViewTitleChanged(ViewProperties*)) );
129
        guiFactory()->removeClient(_pluggedController);
130 131
    }

132 133
    Q_ASSERT( controller );

134 135 136 137 138
    connect( controller , SIGNAL(titleChanged(ViewProperties*)) , 
            this , SLOT(activeViewTitleChanged(ViewProperties*)) );

    guiFactory()->addClient(controller);

139 140 141 142
    // set the current session's search bar
    controller->setSearchBar( searchBar() );
    controller->setShowMenuAction( _toggleMenuBarAction );

143 144 145 146 147 148 149 150 151
    // update session title to match newly activated session
    activeViewTitleChanged(controller);

    _pluggedController = controller;
}

void MainWindow::activeViewTitleChanged(ViewProperties* properties)
{
    setPlainCaption(properties->title());
152
    setWindowIcon(properties->icon());
153 154
}

155
IncrementalSearchBar* MainWindow::searchBar() const
156 157 158 159
{
    return _searchBar;
}

160
void MainWindow::setupActions()
161
{
162 163
    KActionCollection* collection = actionCollection();

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

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

177 178 179 180 181 182
    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()) ); 

183
       
184
#ifndef KONSOLE_PART 
185 186 187 188
    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);
189
#endif
190 191

    // Bookmark Menu
192
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
193
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
194
    collection->addAction("bookmark" , bookmarkMenu);
195

196 197 198
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

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

208 209 210 211 212
    // Hide the Show/Hide menubar item if the menu bar is a MacOS-style menu bar
    if ( menuBar()->isTopLevelMenu() )
        _toggleMenuBarAction->setVisible(false);

    // Full Screen
213 214
    KToggleFullScreenAction* fullScreenAction = new KToggleFullScreenAction(this);
    fullScreenAction->setWindow(this);
215
    fullScreenAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_F11 );
216 217
    collection->addAction("view-full-screen",fullScreenAction);
    connect( fullScreenAction , SIGNAL(toggled(bool)) , this , SLOT(viewFullScreen(bool)) ); 
218
  
219
    // Settings Menu
220 221
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
222 223 224

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

228 229
}

230 231 232 233 234 235 236
void MainWindow::viewFullScreen(bool fullScreen) 
{
    if ( fullScreen )
        showFullScreen();
    else
        showNormal();
}
237
BookmarkHandler* MainWindow::bookmarkHandler() const
238 239 240 241
{
    return _bookmarkHandler;
}

242
void MainWindow::setSessionList(ProfileList* list)
243
{
244
    sessionListChanged(list->actions());
245

246 247
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
248 249 250 251 252 253 254

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
255 256
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
257 258
}

259 260 261 262 263 264 265 266
QString MainWindow::activeSessionDir() const
{
    if ( _pluggedController )
        return _pluggedController->currentDir();
    else
        return QString();
}

267
void MainWindow::newTab()
268
{
269
    emit newSessionRequest( _defaultProfile , activeSessionDir() , _viewManager);
270 271
}

272
void MainWindow::newWindow()
273
{
274
    emit newWindowRequest( _defaultProfile , activeSessionDir() );
275 276
}

277
void MainWindow::showShortcutsDialog()
278
{
279 280
    KShortcutsDialog::configure( actionCollection() ,
                                 KShortcutsEditor::LetterShortcutsDisallowed );
281 282
}

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

293 294 295 296
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
297
        emit newSessionRequest(dialog.sessionKey(),QString(),_viewManager);
298 299
}

300
void MainWindow::setupWidgets()
301
{
302 303 304 305 306
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

308 309 310 311
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
312

313 314 315
    widget->setLayout(layout);
    
    setCentralWidget(widget);
316
}
317

318
#include "MainWindow.moc"