MainWindow.cpp 9.24 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
// Qt
21
#include <QApplication>
22 23
#include <QVBoxLayout>

24 25 26 27
// KDE
#include <KAction>
#include <KActionCollection>
#include <KActionMenu>
28
#include <KShortcutsDialog>
29 30
#include <KLocale>
#include <KMenu>
31
#include <KMenuBar>
32 33
#include <KService>
#include <KToolInvocation>
34
#include <kstandardaction.h>
35
#include <KXMLGUIFactory>
36 37

// Konsole
38 39 40 41
#ifndef KONSOLE_PART
    #include "Application.h"
#endif

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

52 53
using namespace Konsole;

54
MainWindow::MainWindow()
55
 : KXmlGuiWindow() ,
56 57
   _bookmarkHandler(0),
   _pluggedController(0)
58
{
59 60 61
    // add a small amount of space between the top of the window and the main widget
    // to prevent the menu bar and main widget borders touching (which looks very ugly) in styles
    // where the menu bar has a lower border
62
    //setContentsMargins(0,2,0,0);
63
   
64
    // create actions for menus
65
    setXMLFile("konsole/konsoleui.rc");
66 67 68
    setupActions();

    // create view manager
69 70
    // 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.
71
    //setXMLFile("konsole/konsoleui.rc");
72
    _viewManager = new ViewManager(this,actionCollection());
Robert Knight's avatar
 
Robert Knight committed
73
    connect( _viewManager , SIGNAL(empty()) , this , SLOT(close()) );
74 75 76 77
    connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
            SLOT(activeViewChanged(SessionController*)) );
    connect( _viewManager , SIGNAL(viewPropertiesChanged(const QList<ViewProperties*>&)) ,
           bookmarkHandler() , SLOT(setViews(const QList<ViewProperties*>&)) ); 
78

79 80 81
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

82 83 84
    // create main window widgets
    setupWidgets();

85 86 87 88
    // create menus
    createGUI();
}

89
ViewManager* MainWindow::viewManager() const
90 91 92 93
{
    return _viewManager;
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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*)) );
113
        guiFactory()->removeClient(_pluggedController);
114 115
    }

116 117
    Q_ASSERT( controller );

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    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());
}

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

139
void MainWindow::setupActions()
140
{
141 142
    KActionCollection* collection = actionCollection();

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

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

156 157 158 159
    QAction* newFromProfileAction = collection->addAction("new-from-profile");
    newFromProfileAction->setText( i18n("New from Profile...") );
    // TODO Implement "New from Profile"

160 161 162 163 164 165
    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()) ); 

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

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

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

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

184 185 186 187 188
    //TODO - Implmement this correctly
    //
    //QAction* mergeAction = collection->addAction("merge-windows");
    //mergeAction->setText( i18n("&Merge Windows") );
    //connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );
189

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

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

198 199
}

200
BookmarkHandler* MainWindow::bookmarkHandler() const
201 202 203 204
{
    return _bookmarkHandler;
}

205
void MainWindow::setSessionList(ProfileList* list)
206
{
207
    sessionListChanged(list->actions());
208

209 210
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
211 212 213 214 215 216 217

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
218 219
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
220 221
}

222
void MainWindow::newTab()
223 224 225 226
{
    emit requestSession(QString(),_viewManager);
}

227
void MainWindow::newWindow()
228
{
229
#ifndef KONSOLE_PART
230
    Application::self()->newInstance();
231
#endif
232 233
}

234
void MainWindow::showShortcutsDialog()
235
{
236
    KShortcutsDialog::configure( actionCollection() );
237 238
}

239
void MainWindow::newFromProfile(const QString& key)
240 241 242
{
    emit requestSession(key,_viewManager);
}
243
void MainWindow::showManageProfilesDialog()
244
{
245
    ManageProfilesDialog dialog(this);
246 247
    dialog.exec();
}
248

249 250 251 252 253 254 255
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
        emit requestSession(dialog.sessionKey(),_viewManager);
}

256
void MainWindow::mergeWindows()
257
{
Robert Knight's avatar
Robert Knight committed
258 259 260 261
    // merges all of the open Konsole windows into this window
    // by merging the view manager associated with the other Konsole windows
    // into this window's view manager

262
    QListIterator<QWidget*> topLevelIter( QApplication::topLevelWidgets() );
263 264 265

    while (topLevelIter.hasNext())
    {
266 267 268
        QWidget* w = topLevelIter.next();
        qDebug() << "Top level widget: " << w->metaObject()->className();
        MainWindow* window = qobject_cast<MainWindow*>(w);
269 270 271
        if ( window && window != this )
        {
            _viewManager->merge( window->_viewManager );
272
            //window->close();
273 274
        }
    }
275 276
}

277
void MainWindow::setupWidgets()
278
{
279 280 281 282 283
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

285 286 287 288
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
289

290 291 292
    widget->setLayout(layout);
    
    setCentralWidget(widget);
293
}
294

295
#include "MainWindow.moc"