/***************************************************************************
* Copyright (C) 2008 by Tomasz Stelmach *
* http://www.stelmach-online.net/ *
* *
* 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "mainwindow.h"
#include "childwindowfactory.h"
#include "preferences/preferencesdialog.h"
/*!
* Constructor of the MainWindow class
*/
MainWindow::MainWindow()
: QMainWindow()
{
// Create MDI Area
mp_mdiarea = new QMdiArea( this );
mp_mdiarea->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
mp_mdiarea->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded );
setCentralWidget( mp_mdiarea );
// Connect notification
QObject::connect( mp_mdiarea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateActions()) );
// App icon
setWindowIcon( QIcon( ":res/m-ping" ) );
// Create Project Handler
mp_project = new ProjectHandler( mp_mdiarea );
// Create About Window
mp_about_window = new AboutWindow( this );
// Create Actions first
createActions();
// Create Toolbars
createToolbars();
// Create Menu
createMenu();
// Restore state
loadState();
// Additional initialization
updateTitleBar();
updateActions();
}
/*!
* Destructor of the MainWindow class
*/
MainWindow::~MainWindow()
{
// Clear non-Qt memory
delete mp_project;
}
/*!
* \see QWidget::closeEvent( QCloseEvent * event )
*/
void MainWindow::closeEvent( QCloseEvent * event )
{
LOG( "Closing main window" );
// Save state
saveState();
// Close about window and close myself
mp_about_window->close();
QMainWindow::closeEvent( event );
}
/*!
* This method creates actions used in the window
*/
void MainWindow::createActions()
{
// Project -> Add New Ping Window
mp_file_add_new_ping = new QAction( QIcon( ":res/icon_new_ping" ), strings::MENU_FILE_ADD_PING, this );
mp_file_add_new_ping->setToolTip( strings::MENU_FILE_ADD_PING_TT );
QObject::connect( mp_file_add_new_ping, SIGNAL(triggered(bool)), this, SLOT(newPingWindow()) );
// Project -> Add New Trace Window
mp_file_add_new_trace = new QAction( QIcon( ":res/icon_new_traceroute" ), strings::MENU_FILE_ADD_TRACE, this );
mp_file_add_new_trace->setToolTip( strings::MENU_FILE_ADD_TRACE_TT );
QObject::connect( mp_file_add_new_trace, SIGNAL(triggered(bool)), this, SLOT(newTraceWindow()) );
// Project -> Add New Whois Window
mp_file_add_new_whois = new QAction( QIcon( ":res/icon_new_whois" ), strings::MENU_FILE_ADD_WHOIS, this );
mp_file_add_new_whois->setToolTip( strings::MENU_FILE_ADD_WHOIS_TT );
QObject::connect( mp_file_add_new_whois, SIGNAL(triggered(bool)), this, SLOT(newWhoisWindow()) );
// Project -> Load
mp_file_project_load = new QAction( QIcon( ":res/icon_project_open" ), strings::MENU_FILE_OPEN, this );
QObject::connect( mp_file_project_load, SIGNAL(triggered(bool)), this, SLOT(onProjectOpen()) );
// Project -> Save
mp_file_project_save = new QAction( QIcon( ":res/icon_project_save" ), strings::MENU_FILE_SAVE, this );
mp_file_project_save->setShortcut( strings::KEY_FILE_SAVE );
QObject::connect( mp_file_project_save, SIGNAL(triggered(bool)), this, SLOT(onProjectSave()) );
// Project ->Save As
mp_file_project_save_as = new QAction( QIcon( ":res/icon_project_save_as" ), strings::MENU_FILE_SAVE_AS, this );
QObject::connect( mp_file_project_save_as, SIGNAL(triggered(bool)), this, SLOT(onProjectSaveAs()) );
// Project -> Close
mp_file_project_close = new QAction( QIcon( ":res/icon_project_close" ), strings::MENU_FILE_CLOSE, this );
QObject::connect( mp_file_project_close, SIGNAL(triggered(bool)), this, SLOT(onProjectClose()) );
// Project -> Exit
mp_file_project_exit = new QAction( QIcon( ":res/icon_exit" ), strings::MENU_FILE_EXIT, this );
QObject::connect( mp_file_project_exit, SIGNAL(triggered(bool)), this, SLOT(close()));
// Project -> Preferences
mp_file_project_preferences = new QAction( QIcon( ":res/icon_preferences" ), strings::MENU_FILE_PREFERENCES, this );
mp_file_project_preferences->setShortcut( strings::KEY_FILE_PREFERENCES );
QObject::connect( mp_file_project_preferences, SIGNAL(triggered(bool)), this, SLOT(showPreferences()));
// Action -> Start All
mp_action_start_all = new QAction( QIcon( ":res/icon_start_all" ), strings::MENU_ACTION_START_ALL, this );
mp_action_start_all->setShortcuts( strings::KEY_ACTION_START_ALL );
QObject::connect( mp_action_start_all, SIGNAL(triggered(bool)), this, SLOT(onStartAll()));
// Action -> Stop All
mp_action_stop_all = new QAction( QIcon( ":res/icon_stop_all" ), strings::MENU_ACTION_STOP_ALL, this );
mp_action_stop_all->setShortcut( strings::KEY_ACTION_STOP_ALL );
QObject::connect( mp_action_stop_all, SIGNAL(triggered(bool)), this, SLOT(onStopAll()));
// Help -> About
mp_help_about = new QAction( QIcon( ":res/icon_help_about" ), strings::ACTION_HELP_ABOUT, this );
mp_help_about->setShortcut( strings::KEY_HELP_ABOUT );
QObject::connect( mp_help_about, SIGNAL(triggered(bool)), this, SLOT(helpAbout()));
}
/*!
* This method creates toolbars inside the window
*/
void MainWindow::createToolbars()
{
/* Create File toolbar */
mp_filetoolbar = addToolBar( strings::TOOLBAR_FILE );
// Add 'add' button
mp_newbtn = new QToolButton();
mp_toolbar_new_menu = new QMenu();
mp_toolbar_new_menu->addAction( mp_file_add_new_ping );
mp_toolbar_new_menu->addAction( mp_file_add_new_trace );
mp_toolbar_new_menu->addAction( mp_file_add_new_whois );
mp_newbtn->setMenu( mp_toolbar_new_menu );
mp_newbtn->setDefaultAction( mp_file_add_new_ping );
mp_newbtn->setPopupMode( QToolButton::MenuButtonPopup );
mp_filetoolbar->addWidget( mp_newbtn );
// The other project buttons
mp_filetoolbar->addAction( mp_file_project_load );
mp_filetoolbar->addAction( mp_file_project_save );
mp_filetoolbar->addAction( mp_file_project_save_as );
mp_filetoolbar->addAction( mp_file_project_close );
mp_filetoolbar->addSeparator();
mp_filetoolbar->addAction( mp_action_start_all );
mp_filetoolbar->addAction( mp_action_stop_all );
mp_filetoolbar->addSeparator();
mp_filetoolbar->addAction( mp_file_project_preferences );
mp_filetoolbar->addSeparator();
mp_filetoolbar->addAction( mp_help_about );
/* Create Ping Toolbar */
mp_pingtoolbar = addToolBar( strings::TOOLBAR_PING );
/* Create Whois Toolbar */
mp_whoistoolbar = addToolBar( strings::TOOLBAR_WHOIS );
}
/*!
* This method creates menus of the window
*/
void MainWindow::createMenu()
{
// Create menu bar
mp_menubar = new QMenuBar( this );
setMenuBar( mp_menubar );
// Add file menu
mp_menu_file = mp_menubar->addMenu( strings::MENU_FILE );
mp_menu_file_add = mp_menu_file->addMenu( strings::MENU_FILE_ADD );
mp_menu_file_a