output the start and end of autotest testing
[ambit.git] / src / view.cpp
blobdaff9115fe15ef71e3d30ea155cd88d6e6c3189c
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()
41 void View::createViews(QFileSystemModel *model)
43 this->model = model;
44 IconView *iconView = new IconView(this);
45 addView(iconView);
46 TreeView *treeView = new TreeView(this);
47 addView(treeView);
48 ColumnView *columnView = new ColumnView(this);
49 addView(columnView);
52 QModelIndex View::rootIndex() const
54 if (!model)
55 return QModelIndex();
56 return currentView()->rootIndex();
59 void View::setCurrentMode(ViewMode mode)
61 if (!views.isEmpty())
62 setCurrentWidget(views.at((int)mode));
65 View::ViewMode View::currentMode() const
67 if (currentIndex() > 0)
68 return (ViewMode)(currentIndex());
69 return Icon;
72 QAbstractItemView *View::currentView() const
74 if (views.isEmpty())
75 return 0;
76 return views.at(currentIndex());
79 void View::fileOpen()
81 if (!model)
82 return;
83 QModelIndex index = currentView()->currentIndex();
84 if (!index.isValid())
85 return;
86 if (model->hasChildren(index)) {
87 QMetaObject::invokeMethod(currentView(), "openIndex", Q_ARG(QModelIndex, index));
88 // TODO root index might change
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()));