13501
|
1 /* Quint - A graphical user interface for Octave |
|
2 * Copyright (C) 2011 Jacob Dawid |
|
3 * jacob.dawid@googlemail.com |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "FilesDockWidget.h" |
|
20 |
|
21 #include <QApplication> |
|
22 #include <QFileInfo> |
|
23 #include <QCompleter> |
|
24 |
|
25 FilesDockWidget::FilesDockWidget(QWidget *parent) |
|
26 : QDockWidget(parent) { |
|
27 setObjectName("FilesDockWidget"); |
|
28 setWindowTitle(tr("Current Folder")); |
|
29 setWidget(new QWidget(this)); |
|
30 |
|
31 // Create a toolbar |
|
32 m_navigationToolBar = new QToolBar("", widget()); |
|
33 m_navigationToolBar->setAllowedAreas(Qt::TopToolBarArea); |
|
34 m_navigationToolBar->setMovable(false); |
|
35 m_navigationToolBar->setIconSize(QSize (20,20)); |
|
36 |
|
37 // Add a button to the toolbar with the QT standard icon for up-directory |
|
38 // TODO: Maybe change this to be an up-directory icon that is OS specific??? |
|
39 QStyle *style = QApplication::style(); |
|
40 m_directoryIcon = style->standardIcon(QStyle::SP_FileDialogToParent); |
|
41 m_directoryUpAction = new QAction(m_directoryIcon, "", m_navigationToolBar); |
|
42 m_currentDirectory = new QLineEdit(m_navigationToolBar); |
|
43 |
|
44 m_navigationToolBar->addAction(m_directoryUpAction); |
|
45 m_navigationToolBar->addWidget(m_currentDirectory); |
|
46 connect(m_directoryUpAction, SIGNAL(triggered()), this, SLOT(onUpDirectory())); |
|
47 |
|
48 // TODO: Add other buttons for creating directories |
|
49 |
|
50 // Create the QFileSystemModel starting in the home directory |
|
51 QString homePath = QDir::homePath(); |
|
52 // TODO: This should occur after Octave has been initialized and the startup directory of Octave is established |
|
53 |
|
54 m_fileSystemModel = new QFileSystemModel(this); |
|
55 m_fileSystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllEntries); |
|
56 QModelIndex rootPathIndex = m_fileSystemModel->setRootPath(homePath); |
|
57 |
|
58 // Attach the model to the QTreeView and set the root index |
|
59 m_fileTreeView = new QTreeView(widget()); |
|
60 m_fileTreeView->setModel(m_fileSystemModel); |
|
61 m_fileTreeView->setRootIndex(rootPathIndex); |
|
62 m_fileTreeView->setSortingEnabled(true); |
|
63 m_fileTreeView->setAlternatingRowColors(true); |
|
64 m_fileTreeView->setAnimated(true); |
|
65 setCurrentDirectory(m_fileSystemModel->fileInfo(rootPathIndex).absoluteFilePath()); |
|
66 |
|
67 connect(m_fileTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(itemDoubleClicked(const QModelIndex &))); |
|
68 |
|
69 // Layout the widgets vertically with the toolbar on top |
|
70 QVBoxLayout *layout = new QVBoxLayout(); |
|
71 layout->setSpacing(0); |
|
72 layout->addWidget(m_navigationToolBar); |
|
73 layout->addWidget(m_fileTreeView); |
|
74 widget()->setLayout(layout); |
|
75 // TODO: Add right-click contextual menus for copying, pasting, deleting files (and others) |
|
76 |
|
77 connect(m_currentDirectory, SIGNAL(returnPressed()), this, SLOT(currentDirectoryEntered())); |
|
78 QCompleter *completer = new QCompleter(m_fileSystemModel, this); |
|
79 m_currentDirectory->setCompleter(completer); |
|
80 } |
|
81 |
|
82 void FilesDockWidget::itemDoubleClicked(const QModelIndex &index) |
|
83 { |
|
84 QFileInfo fileInfo = m_fileSystemModel->fileInfo(index); |
|
85 if (fileInfo.isDir()) { |
|
86 m_fileSystemModel->setRootPath(fileInfo.absolutePath()); |
|
87 m_fileTreeView->setRootIndex(index); |
|
88 setCurrentDirectory(m_fileSystemModel->fileInfo(index).absoluteFilePath()); |
|
89 } else { |
|
90 QFileInfo fileInfo = m_fileSystemModel->fileInfo(index); |
|
91 emit openFile(fileInfo.filePath()); |
|
92 } |
|
93 } |
|
94 |
|
95 void FilesDockWidget::setCurrentDirectory(QString currentDirectory) { |
|
96 m_currentDirectory->setText(currentDirectory); |
|
97 } |
|
98 |
|
99 void FilesDockWidget::onUpDirectory(void) { |
|
100 QDir dir = QDir(m_fileSystemModel->filePath(m_fileTreeView->rootIndex())); |
|
101 dir.cdUp(); |
|
102 m_fileSystemModel->setRootPath(dir.absolutePath()); |
|
103 m_fileTreeView->setRootIndex(m_fileSystemModel->index(dir.absolutePath())); |
|
104 setCurrentDirectory(dir.absolutePath()); |
|
105 } |
|
106 |
|
107 void FilesDockWidget::currentDirectoryEntered() { |
|
108 QFileInfo fileInfo(m_currentDirectory->text()); |
|
109 if (fileInfo.isDir()) { |
|
110 m_fileTreeView->setRootIndex(m_fileSystemModel->index(fileInfo.absolutePath())); |
|
111 m_fileSystemModel->setRootPath(fileInfo.absolutePath()); |
|
112 setCurrentDirectory(fileInfo.absoluteFilePath()); |
|
113 } else { |
|
114 if(QFile::exists(fileInfo.absoluteFilePath())) |
|
115 emit openFile(fileInfo.absoluteFilePath()); |
|
116 } |
|
117 } |