Refactor out the view into its own class and add autotest for it
[ambit.git] / src / mainwindow.cpp
blob1679098288449e6e00a7b7d2438176e80de9ab11
1 /**
2 * Copyright (C) 2007 Benjamin C. Meyer
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "mainwindow.h"
21 #include "view.h"
22 #include "aboutdialog.h"
24 #include "history.h"
25 #include "qsidebar.h"
26 #include "diskinformation.h"
27 #include "qfilesystemmodel_p.h"
29 #include <QtGui/QtGui>
31 MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent), model(0)
33 //setAttribute(Qt::WA_MacMetalStyle, true);
34 model = new QFileSystemModel(this);
35 model->setReadOnly(false);
37 createWidgets();
38 createActions();
39 createToolBar();
40 createMenu();
42 setRootIndex(model->index(QDir::homePath()));
45 MainWindow::~MainWindow()
49 void MainWindow::createWidgets()
51 toolBar = addToolBar(QLatin1String("Toolbar"));
53 splitter = new QSplitter(this);
55 QList<QUrl> initialList;
56 initialList << QUrl::fromLocalFile("/");
57 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Desktop");
58 initialList << QUrl::fromLocalFile(QDir::homePath());
59 initialList << QUrl::fromLocalFile("/Applications");
60 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Documents");
61 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Movies");
62 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Music");
63 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Pictures");
64 sidebar = new QSidebar(model, initialList, this);
65 connect(sidebar, SIGNAL(goTo(const QModelIndex &)),
66 this, SLOT(setRootIndex(const QModelIndex &)));
68 view = new View(this);
69 view->createViews(model);
70 connect(view, SIGNAL(currentFolderChanged(const QModelIndex &)),
71 sidebar, SLOT(selectIndex(const QModelIndex &)));
73 splitter->addWidget(sidebar);
74 splitter->addWidget(view);
75 splitter->setStretchFactor(splitter->indexOf(view), QSizePolicy::Expanding);
76 setCentralWidget(splitter);
78 historyActionsGroup = new QActionGroup(this);
79 historyActionsGroup->setExclusive(true);
81 viewActionsGroup = new QActionGroup(this);
82 viewActionsGroup->setExclusive(true);
83 connect(viewActionsGroup, SIGNAL(triggered(QAction *)), this, SLOT(showView(QAction *)));
85 statusBarLabel = new QLabel(statusBar());
86 statusBarLabel->setAlignment(Qt::AlignCenter);
87 statusBar()->addWidget(statusBarLabel, 1);
90 void MainWindow::createActions()
92 about = new QAction(tr("About ") + qApp->applicationName(), this);
93 about->setIcon(qApp->windowIcon());
94 about->setMenuRole(QAction::AboutRole);
95 connect(about, SIGNAL(triggered()), this, SLOT(showAbout()));
97 fileOpenAction = new QAction(tr("Open"), this);
98 fileOpenAction->setShortcut(tr("Ctrl+O"));
99 connect(fileOpenAction, SIGNAL(triggered()), view, SLOT(fileOpen()));
101 fileCloseAction = new QAction(tr("Close Window"), this);
102 fileCloseAction->setShortcut(tr("Ctrl+W"));
103 connect(fileCloseAction, SIGNAL(triggered()), this, SLOT(close()));
105 fileQuitAction = new QAction(tr("Quit"), this);
106 fileQuitAction->setShortcut(tr("Ctrl+Q"));
107 connect(fileQuitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
109 QAction *iconAction = new QAction(QIcon(":/pics/iconview.png"), "as Icons", this);
110 viewActionsGroup->addAction(iconAction);
111 iconAction->setCheckable(true);
112 iconAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
113 iconAction->setChecked(true);
115 QAction *treeAction = new QAction(QIcon(":/pics/treeview.png"), "as List", this);
116 viewActionsGroup->addAction(treeAction);
117 treeAction->setCheckable(true);
118 treeAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
120 QAction *columnAction = new QAction(QIcon(":/pics/columnview.png"), "as Columns", this);
121 viewActionsGroup->addAction(columnAction);
122 columnAction->setCheckable(true);
123 columnAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
125 goUpAction = new QAction(tr("Enclosing Folder"), toolBar);
126 goUpAction->setShortcut(Qt::CTRL + Qt::Key_Up);
127 goUpAction->setIcon(style()->standardIcon(QStyle::SP_ArrowUp));
128 goUpAction->setEnabled(false);
129 connect(goUpAction, SIGNAL(triggered()), this, SLOT(goUp()));
131 minimizeWindowAction = new QAction(tr("&Minimize"), this);
132 minimizeWindowAction->setShortcut(tr("Ctrl+M"));
133 connect(minimizeWindowAction, SIGNAL(triggered()), this, SLOT(showMinimized()));
134 // TODO disable minimize when minimized
136 QAction *action = toolBar->toggleViewAction();
137 action->setShortcut(Qt::CTRL + Qt::Key_T + Qt::ALT);
138 // TODO text should say "show" or "hide"
140 backAction = History::backAction(this);
141 connect(backAction, SIGNAL(triggered()), view->history, SLOT(goBack()));
142 forwardAction = History::forwardAction(this);
143 connect(forwardAction, SIGNAL(triggered()), view->history, SLOT(goForward()));
144 view->history->goBackAction = backAction;
145 view->history->goForwardAction = forwardAction;
148 void MainWindow::arrangeBy(QAction *action)
150 model->sort(arrangeByActionGroup->actions().indexOf(action), Qt::AscendingOrder);
153 void MainWindow::createMenu()
155 fileMenu = menuBar()->addMenu(tr("&File"));
156 fileMenu->addAction(fileOpenAction);
157 fileMenu->addAction(fileCloseAction);
158 fileMenu->addAction(fileQuitAction);
160 editMenu = menuBar()->addMenu(tr("&Edit"));
162 // View
163 viewMenu = menuBar()->addMenu(tr("&View"));
164 for (int i = 0; i < viewActionsGroup->actions().count(); ++i)
165 viewMenu->addAction(viewActionsGroup->actions().at(i));
166 viewMenu->addSeparator();
168 QMenu *arrangeByMenu = viewMenu->addMenu(tr("Arrange By"));
169 connect(arrangeByMenu, SIGNAL(triggered(QAction *)),
170 this, SLOT(arrangeBy(QAction *)));
172 arrangeByActionGroup = new QActionGroup(this);
173 for (int i = 0; i < model->columnCount(); ++i)
174 new QAction(model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(), arrangeByActionGroup);
175 arrangeByMenu->addActions(arrangeByActionGroup->actions());
177 viewMenu->addSeparator();
178 viewMenu->addAction(toolBar->toggleViewAction());
180 // Go
181 goMenu = menuBar()->addMenu(tr("Go"));
182 goMenu->addAction(backAction);
183 goMenu->addAction(forwardAction);
184 goMenu->addAction(goUpAction);
185 goMenu->addSeparator();
187 windowMenu = menuBar()->addMenu(tr("Window"));
188 windowMenu->addAction(minimizeWindowAction);
190 helpMenu = menuBar()->addMenu(tr("Help"));
191 helpMenu->addAction(about);
194 void MainWindow::createToolBar()
196 toolBar->setMovable(false);
197 toolBar->addAction(backAction);
198 toolBar->addAction(forwardAction);
199 for (int i = 0; i < viewActionsGroup->actions().count(); ++i)
200 toolBar->addAction(viewActionsGroup->actions().at(i));
201 setUnifiedTitleAndToolBarOnMac(true);
204 void MainWindow::showView(QAction *action)
206 for (int i = 0; i < viewActionsGroup->actions().count(); ++i) {
207 if (viewActionsGroup->actions().at(i) == action) {
208 view->setCurrentMode((View::ViewMode)(i));
209 break;
214 void MainWindow::goUp()
216 view->goUp();
219 void MainWindow::setRootIndex(const QModelIndex &index)
221 view->setRootIndex(index);
222 goUpAction->setEnabled(index.parent().isValid());
223 setWindowIcon(index.data(Qt::DecorationRole).value<QIcon>());
224 setWindowTitle(index.data().toString());
225 // update statusbar
226 qint64 freeSpace = getDiskInformation(index.data(QFileSystemModel::FilePathRole).toString()).free;
227 QString freeSpaceString = humanSize(freeSpace);
228 statusBarLabel->setText(QString("%1 available").arg(freeSpaceString));
231 void MainWindow::showAbout()
233 AboutDialog *aboutDialog = new AboutDialog(this);
234 aboutDialog->setVersion("Version 0.1");
235 QStringList authors;
236 authors << "© 2007 Benjamin C Meyer <a href=\"mailto:ben@meyerhome.net\">ben@meyerhome.net</a>";
237 aboutDialog->addAuthors(authors);
238 aboutDialog->show();