only include Applications by default on OS X
[ambit.git] / src / mainwindow.cpp
blob764a1abab102c8a2ef3bb3d2fa721189e6bde83f
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 #ifdef Q_OS_MAC
60 initialList << QUrl::fromLocalFile("/Applications");
61 #endif
62 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Documents");
63 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Movies");
64 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Music");
65 initialList << QUrl::fromLocalFile(QDir::homePath() + "/Pictures");
66 sidebar = new QSidebar(model, initialList, this);
67 connect(sidebar, SIGNAL(goTo(const QModelIndex &)),
68 this, SLOT(setRootIndex(const QModelIndex &)));
70 view = new View(this);
71 view->createViews(model);
72 connect(view, SIGNAL(currentFolderChanged(const QModelIndex &)),
73 sidebar, SLOT(selectIndex(const QModelIndex &)));
75 splitter->addWidget(sidebar);
76 splitter->addWidget(view);
77 splitter->setStretchFactor(splitter->indexOf(view), QSizePolicy::Expanding);
78 setCentralWidget(splitter);
80 historyActionsGroup = new QActionGroup(this);
81 historyActionsGroup->setExclusive(true);
83 viewActionsGroup = new QActionGroup(this);
84 viewActionsGroup->setExclusive(true);
85 connect(viewActionsGroup, SIGNAL(triggered(QAction *)), this, SLOT(showView(QAction *)));
87 statusBarLabel = new QLabel(statusBar());
88 statusBarLabel->setAlignment(Qt::AlignCenter);
89 statusBar()->addWidget(statusBarLabel, 1);
92 void MainWindow::createActions()
94 about = new QAction(tr("About ") + qApp->applicationName(), this);
95 about->setIcon(qApp->windowIcon());
96 about->setMenuRole(QAction::AboutRole);
97 connect(about, SIGNAL(triggered()), this, SLOT(showAbout()));
99 fileOpenAction = new QAction(tr("Open"), this);
100 fileOpenAction->setShortcut(tr("Ctrl+O"));
101 connect(fileOpenAction, SIGNAL(triggered()), view, SLOT(fileOpen()));
103 fileCloseAction = new QAction(tr("Close Window"), this);
104 fileCloseAction->setShortcut(tr("Ctrl+W"));
105 connect(fileCloseAction, SIGNAL(triggered()), this, SLOT(close()));
107 fileQuitAction = new QAction(tr("Quit"), this);
108 fileQuitAction->setShortcut(tr("Ctrl+Q"));
109 connect(fileQuitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
111 QAction *iconAction = new QAction(QIcon(":/pics/iconview.png"), "as Icons", this);
112 viewActionsGroup->addAction(iconAction);
113 iconAction->setCheckable(true);
114 iconAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
115 iconAction->setChecked(true);
117 QAction *treeAction = new QAction(QIcon(":/pics/treeview.png"), "as List", this);
118 viewActionsGroup->addAction(treeAction);
119 treeAction->setCheckable(true);
120 treeAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
122 QAction *columnAction = new QAction(QIcon(":/pics/columnview.png"), "as Columns", this);
123 viewActionsGroup->addAction(columnAction);
124 columnAction->setCheckable(true);
125 columnAction->setShortcut((QString("Ctrl+%1").arg(viewActionsGroup->actions().count())));
127 goUpAction = new QAction(tr("Enclosing Folder"), toolBar);
128 goUpAction->setShortcut(Qt::CTRL + Qt::Key_Up);
129 goUpAction->setIcon(style()->standardIcon(QStyle::SP_ArrowUp));
130 goUpAction->setEnabled(false);
131 connect(goUpAction, SIGNAL(triggered()), this, SLOT(goUp()));
133 minimizeWindowAction = new QAction(tr("&Minimize"), this);
134 minimizeWindowAction->setShortcut(tr("Ctrl+M"));
135 connect(minimizeWindowAction, SIGNAL(triggered()), this, SLOT(showMinimized()));
136 // TODO disable minimize when minimized
138 QAction *action = toolBar->toggleViewAction();
139 action->setShortcut(Qt::CTRL + Qt::Key_T + Qt::ALT);
140 // TODO text should say "show" or "hide"
142 backAction = History::backAction(this);
143 connect(backAction, SIGNAL(triggered()), view->history, SLOT(goBack()));
144 forwardAction = History::forwardAction(this);
145 connect(forwardAction, SIGNAL(triggered()), view->history, SLOT(goForward()));
146 view->history->goBackAction = backAction;
147 view->history->goForwardAction = forwardAction;
150 void MainWindow::arrangeBy(QAction *action)
152 model->sort(arrangeByActionGroup->actions().indexOf(action), Qt::AscendingOrder);
155 void MainWindow::createMenu()
157 fileMenu = menuBar()->addMenu(tr("&File"));
158 fileMenu->addAction(fileOpenAction);
159 fileMenu->addAction(fileCloseAction);
160 fileMenu->addAction(fileQuitAction);
162 editMenu = menuBar()->addMenu(tr("&Edit"));
164 // View
165 viewMenu = menuBar()->addMenu(tr("&View"));
166 for (int i = 0; i < viewActionsGroup->actions().count(); ++i)
167 viewMenu->addAction(viewActionsGroup->actions().at(i));
168 viewMenu->addSeparator();
170 QMenu *arrangeByMenu = viewMenu->addMenu(tr("Arrange By"));
171 connect(arrangeByMenu, SIGNAL(triggered(QAction *)),
172 this, SLOT(arrangeBy(QAction *)));
174 arrangeByActionGroup = new QActionGroup(this);
175 for (int i = 0; i < model->columnCount(); ++i)
176 new QAction(model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(), arrangeByActionGroup);
177 arrangeByMenu->addActions(arrangeByActionGroup->actions());
179 viewMenu->addSeparator();
180 viewMenu->addAction(toolBar->toggleViewAction());
182 // Go
183 goMenu = menuBar()->addMenu(tr("Go"));
184 goMenu->addAction(backAction);
185 goMenu->addAction(forwardAction);
186 goMenu->addAction(goUpAction);
187 goMenu->addSeparator();
189 windowMenu = menuBar()->addMenu(tr("Window"));
190 windowMenu->addAction(minimizeWindowAction);
192 helpMenu = menuBar()->addMenu(tr("Help"));
193 helpMenu->addAction(about);
196 void MainWindow::createToolBar()
198 toolBar->setMovable(false);
199 toolBar->addAction(backAction);
200 toolBar->addAction(forwardAction);
201 for (int i = 0; i < viewActionsGroup->actions().count(); ++i)
202 toolBar->addAction(viewActionsGroup->actions().at(i));
203 setUnifiedTitleAndToolBarOnMac(true);
206 void MainWindow::showView(QAction *action)
208 for (int i = 0; i < viewActionsGroup->actions().count(); ++i) {
209 if (viewActionsGroup->actions().at(i) == action) {
210 view->setCurrentMode((View::ViewMode)(i));
211 break;
216 void MainWindow::goUp()
218 view->goUp();
221 void MainWindow::setRootIndex(const QModelIndex &index)
223 view->setRootIndex(index);
224 goUpAction->setEnabled(index.parent().isValid());
225 setWindowIcon(index.data(Qt::DecorationRole).value<QIcon>());
226 setWindowTitle(index.data().toString());
227 // update statusbar
228 qint64 freeSpace = getDiskInformation(index.data(QFileSystemModel::FilePathRole).toString()).free;
229 QString freeSpaceString = humanSize(freeSpace);
230 statusBarLabel->setText(QString("%1 available").arg(freeSpaceString));
233 void MainWindow::showAbout()
235 AboutDialog *aboutDialog = new AboutDialog(this);
236 aboutDialog->setVersion("Version 0.1");
237 QStringList authors;
238 authors << "© 2007 Benjamin C Meyer <a href=\"mailto:ben@meyerhome.net\">ben@meyerhome.net</a>";
239 aboutDialog->addAuthors(authors);
240 aboutDialog->show();