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.
23 #include "columnview.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
&)));
42 void View::createViews(QFileSystemModel
*model
)
45 IconView
*iconView
= new IconView(this);
47 TreeView
*treeView
= new TreeView(this);
49 ColumnView
*columnView
= new ColumnView(this);
53 QModelIndex
View::rootIndex() const
57 return currentView()->rootIndex();
60 void View::setCurrentMode(ViewMode mode
)
63 setCurrentWidget(views
.at((int)mode
));
66 View::ViewMode
View::currentMode() const
68 if (currentIndex() > 0)
69 return (ViewMode
)(currentIndex());
73 QAbstractItemView
*View::currentView() const
77 return views
.at(currentIndex());
84 QModelIndex index
= currentView()->currentIndex();
87 if (model
->hasChildren(index
)) {
88 QMetaObject::invokeMethod(currentView(), "openIndex", Q_ARG(QModelIndex
, index
));
92 QUrl url
= QUrl::fromLocalFile(index
.data(QFileSystemModel::FilePathRole
).toString());
93 QDesktopServices::openUrl(url
);
96 void View::setRootIndex(const QModelIndex
&index
)
100 if (rootIndex() == index
) {
101 qWarning() << index
<< "is the current root";
105 if (!model
->hasChildren(index
)) {
110 for (int i
= 0; i
< views
.count(); ++i
) {
111 views
.at(i
)->setRootIndex(index
);
114 emit
currentFolderChanged(index
);
119 QAbstractItemView
*view
= currentView();
122 QModelIndex current
= view
->currentIndex();
123 QModelIndex root
= view
->rootIndex();
124 if (current
.isValid() && current
.parent() != root
)
126 root
= root
.parent();
127 view
->setCurrentIndex(root
);
128 if (view
->visualRect(root
).isNull())
129 view
->setRootIndex(root
);
132 void View::addView(QAbstractItemView
*view
)
135 view
->setSelectionBehavior(QAbstractItemView::SelectRows
);
136 view
->setModel(model
);
139 if (!views
.isEmpty())
140 view
->setSelectionModel(views
.first()->selectionModel());
141 connect(view
, SIGNAL(doubleClicked(const QModelIndex
&)),
142 this, SLOT(fileOpen()));