MainWindow.cpp 8.87 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 25
#include <QtGui/QApplication>
#include <QtGui/QBoxLayout>
26

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

// Konsole
42 43 44 45
#ifndef KONSOLE_PART
    #include "Application.h"
#endif

46
#include "BookmarkHandler.h"
47 48
#include "IncrementalSearchBar.h"
#include "RemoteConnectionDialog.h"
49
#include "SessionController.h"
50 51
#include "ProfileList.h"
#include "ManageProfilesDialog.h"
52 53
#include "ViewManager.h"
#include "ViewSplitter.h"
54

55 56
using namespace Konsole;

57
MainWindow::MainWindow()
58
 : KXmlGuiWindow() ,
59 60
   _bookmarkHandler(0),
   _pluggedController(0)
61
{
62
    // create actions for menus
63 64
    // 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.
65
    setXMLFile("konsole/konsoleui.rc");
66 67 68
    setupActions();

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

76 77 78
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

79 80 81
    // create main window widgets
    setupWidgets();

82 83 84 85
    // create menus
    createGUI();
}

86
ViewManager* MainWindow::viewManager() const
87 88 89 90
{
    return _viewManager;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
void MainWindow::activeViewChanged(SessionController* controller)
{
    if ( _pluggedController == controller )
        return;

    // associated bookmark menu with current session
    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() );

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

113 114
    Q_ASSERT( controller );

115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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());
129
    setWindowIcon(properties->icon());
130 131
}

132
IncrementalSearchBar* MainWindow::searchBar() const
133 134 135 136
{
    return _searchBar;
}

137
void MainWindow::setupActions()
138
{
139 140
    KActionCollection* collection = actionCollection();

141
    // File Menu
142 143 144
    QAction* newTabAction = collection->addAction("new-tab"); 
    newTabAction->setIcon( KIcon("openterm") );
    newTabAction->setText( i18n("New &Tab") );
Robert Knight's avatar
 
Robert Knight committed
145
    newTabAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_N) );
146
    connect( newTabAction , SIGNAL(triggered()) , this , SLOT(newTab()) );
147 148

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

154
    QAction* newFromProfileAction = collection->addAction("new-from-profile");
Stephan Binner's avatar
Stephan Binner committed
155
    newFromProfileAction->setText( i18n("New From Profile...") );
156 157
    // TODO Implement "New from Profile"

158 159 160 161 162 163
    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()) ); 

164
       
165
#ifndef KONSOLE_PART 
166
    KStandardAction::quit( Application::self() , SLOT(quit()) , collection );
167
#endif
168 169

    // Bookmark Menu
170
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
171
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
172
    collection->addAction("bookmark" , bookmarkMenu);
173

174 175 176
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

177 178 179 180 181
    // View Menu
    QAction* hideMenuBarAction = collection->addAction("hide-menubar");
    hideMenuBarAction->setText( i18n("Hide MenuBar") );
    connect( hideMenuBarAction , SIGNAL(triggered()) , menuBar() , SLOT(hide()) );

182 183 184 185
    KToggleFullScreenAction* fullScreenAction = new KToggleFullScreenAction(this);
    fullScreenAction->setWindow(this);
    collection->addAction("view-full-screen",fullScreenAction);
    connect( fullScreenAction , SIGNAL(toggled(bool)) , this , SLOT(viewFullScreen(bool)) ); 
186 187 188 189 190
    //TODO - Implmement this correctly
    //
    //QAction* mergeAction = collection->addAction("merge-windows");
    //mergeAction->setText( i18n("&Merge Windows") );
    //connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );
191

192
    // Settings Menu
193 194
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
195 196 197

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

201 202
}

203 204 205 206 207 208 209
void MainWindow::viewFullScreen(bool fullScreen) 
{
    if ( fullScreen )
        showFullScreen();
    else
        showNormal();
}
210
BookmarkHandler* MainWindow::bookmarkHandler() const
211 212 213 214
{
    return _bookmarkHandler;
}

215
void MainWindow::setSessionList(ProfileList* list)
216
{
217
    sessionListChanged(list->actions());
218

219 220
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
221 222 223 224 225 226 227

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
228 229
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
230 231
}

232
void MainWindow::newTab()
233 234 235 236
{
    emit requestSession(QString(),_viewManager);
}

237
void MainWindow::newWindow()
238
{
239
#ifndef KONSOLE_PART
240
    Application::self()->newInstance();
241
#endif
242 243
}

244
void MainWindow::showShortcutsDialog()
245
{
246
    KShortcutsDialog::configure( actionCollection() );
247 248
}

249
void MainWindow::newFromProfile(const QString& key)
250 251 252
{
    emit requestSession(key,_viewManager);
}
253
void MainWindow::showManageProfilesDialog()
254
{
255 256
    ManageProfilesDialog* dialog = new ManageProfilesDialog(this);
    dialog->show();
257
}
258

259 260 261 262 263 264 265
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
        emit requestSession(dialog.sessionKey(),_viewManager);
}

266
void MainWindow::setupWidgets()
267
{
268 269 270 271 272
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

274 275 276 277
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
278

279 280 281
    widget->setLayout(layout);
    
    setCentralWidget(widget);
282
}
283

284
#include "MainWindow.moc"