Refactor out the view into its own class and add autotest for it
[ambit.git] / src / view.cpp
blobefaae5f3442a811316c0fdd89e96ac07bdb8a0c8
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 "view.h"
21 #include "iconview.h"
22 #include "treeview.h"
23 #include "columnview.h"
24 #include "history.h"
25 #include "qfilesystemmodel_p.h"
27 #include <QtGui/QtGui>
29 View::View(QWidget *parent) : QStackedWidget(parent), model(0)
31 history = new History(this);
32 connect(this, SIGNAL(currentFolderChanged(const QModelIndex &)),
33 history, SLOT(currentChanged(const QModelIndex &)));
34 connect(history, SIGNAL(goToIndex(const QModelIndex &)),
35 this, SLOT(setRootIndex(const QModelIndex &)));
38 View::~View()
42 void View::createViews(QFileSystemModel *model)
44 this->model = model;
45 IconView *iconView = new IconView(this);
46 addView(iconView);
47 TreeView *treeView = new TreeView(this);
48 addView(treeView);
49 ColumnView *columnView = new ColumnView(this);
50 addView(columnView);
53 QModelIndex View::rootIndex() const
55 if (!model)
56 return QModelIndex();
57 return currentView()->rootIndex();
60 void View::setCurrentMode(ViewMode mode)
62 if (!views.isEmpty())
63 setCurrentWidget(views.at((int)mode));
66 View::ViewMode View::currentMode() const
68 if (currentIndex() > 0)
69 return (ViewMode)(currentIndex());
70 return Icon;
73 QAbstractItemView *View::currentView() const
75 if (views.isEmpty())
76 return 0;
77 return views.at(currentIndex());
80 void View::fileOpen()
82 if (!model)
83 return;
84 QModelIndex index = currentView()->currentIndex();
85 if (!index.isValid())
86 return;
87 if (model->hasChildren(index)) {
88 QMetaObject::invokeMethod(currentView(), "openIndex", Q_ARG(QModelIndex, index));
89 return;
92 QUrl url = QUrl::fromLocalFile(index.data(QFileSystemModel::FilePathRole).toString());
93 QDesktopServices::openUrl(url);
96 void View::setRootIndex(const QModelIndex &index)
98 if (!model)
99 return;
100 if (rootIndex() == index) {
101 qWarning() << index << "is the current root";
102 return;
105 if (!model->hasChildren(index)) {
106 fileOpen();
107 return;
110 for (int i = 0; i < views.count(); ++i) {
111 views.at(i)->setRootIndex(index);
114 emit currentFolderChanged(index);
117 void View::goUp()
119 QAbstractItemView *view = currentView();
120 if (!view)
121 return;
122 QModelIndex current = view->currentIndex();
123 QModelIndex root = view->rootIndex();
124 if (current.isValid() && current.parent() != root)
125 root = current;
126 root = root.parent();
127 view->setCurrentIndex(root);
128 if (view->visualRect(root).isNull())
129 view->setRootIndex(root);
132 void View::addView(QAbstractItemView *view)
134 views.append(view);
135 view->setSelectionBehavior(QAbstractItemView::SelectRows);
136 view->setModel(model);
137 addWidget(view);
139 if (!views.isEmpty())
140 view->setSelectionModel(views.first()->selectionModel());
141 connect(view, SIGNAL(doubleClicked(const QModelIndex &)),
142 this, SLOT(fileOpen()));