Mercurial > hg > octave-nkf
annotate gui/src/FilesDockWidget.cpp @ 13522:09d9cb7c3014
Further simplified OctaveLink by removing the readline event hook.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Wed, 20 Jul 2011 00:34:38 +0200 |
parents | c70511cf64ee |
children | bb3676025b36 |
rev | line source |
---|---|
13504
13e3d60aff2d
Replaced Quint with OctaveGUI.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
1 /* OctaveGUI - A graphical user interface for Octave |
13501 | 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 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
25 FilesDockWidget::FilesDockWidget (QWidget * parent):QDockWidget (parent) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
26 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
27 setObjectName ("FilesDockWidget"); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
28 setWindowTitle (tr ("Current Folder")); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
29 setWidget (new QWidget (this)); |
13501 | 30 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
31 // Create a toolbar |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
32 m_navigationToolBar = new QToolBar ("", widget ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
33 m_navigationToolBar->setAllowedAreas (Qt::TopToolBarArea); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
34 m_navigationToolBar->setMovable (false); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
35 m_navigationToolBar->setIconSize (QSize (20, 20)); |
13501 | 36 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
37 // Add a button to the toolbar with the QT standard icon for up-directory |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
38 // TODO: Maybe change this to be an up-directory icon that is OS specific??? |
13522
09d9cb7c3014
Further simplified OctaveLink by removing the readline event hook.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13506
diff
changeset
|
39 QStyle *style = QApplication::style (); |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
40 m_directoryIcon = style->standardIcon (QStyle::SP_FileDialogToParent); |
13522
09d9cb7c3014
Further simplified OctaveLink by removing the readline event hook.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13506
diff
changeset
|
41 m_directoryUpAction = new QAction (m_directoryIcon, "", m_navigationToolBar); |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
42 m_currentDirectory = new QLineEdit (m_navigationToolBar); |
13501 | 43 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
44 m_navigationToolBar->addAction (m_directoryUpAction); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
45 m_navigationToolBar->addWidget (m_currentDirectory); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
46 connect (m_directoryUpAction, SIGNAL (triggered ()), this, |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
47 SLOT (onUpDirectory ())); |
13501 | 48 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
49 // TODO: Add other buttons for creating directories |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
50 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
51 // Create the QFileSystemModel starting in the home directory |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
52 QString |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
53 homePath = QDir::homePath (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
54 // TODO: This should occur after Octave has been initialized and the startup directory of Octave is established |
13501 | 55 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
56 m_fileSystemModel = new QFileSystemModel (this); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
57 m_fileSystemModel->setFilter (QDir::NoDotAndDotDot | QDir::AllEntries); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
58 QModelIndex |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
59 rootPathIndex = m_fileSystemModel->setRootPath (homePath); |
13501 | 60 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
61 // Attach the model to the QTreeView and set the root index |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
62 m_fileTreeView = new QTreeView (widget ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
63 m_fileTreeView->setModel (m_fileSystemModel); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
64 m_fileTreeView->setRootIndex (rootPathIndex); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
65 m_fileTreeView->setSortingEnabled (true); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
66 m_fileTreeView->setAlternatingRowColors (true); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
67 m_fileTreeView->setAnimated (true); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
68 setCurrentDirectory (m_fileSystemModel->fileInfo (rootPathIndex). |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
69 absoluteFilePath ()); |
13501 | 70 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
71 connect (m_fileTreeView, SIGNAL (doubleClicked (const QModelIndex &)), this, |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
72 SLOT (itemDoubleClicked (const QModelIndex &))); |
13501 | 73 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
74 // Layout the widgets vertically with the toolbar on top |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
75 QVBoxLayout * |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
76 layout = new QVBoxLayout (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
77 layout->setSpacing (0); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
78 layout->addWidget (m_navigationToolBar); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
79 layout->addWidget (m_fileTreeView); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
80 widget ()->setLayout (layout); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
81 // TODO: Add right-click contextual menus for copying, pasting, deleting files (and others) |
13501 | 82 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
83 connect (m_currentDirectory, SIGNAL (returnPressed ()), this, |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
84 SLOT (currentDirectoryEntered ())); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
85 QCompleter * |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
86 completer = new QCompleter (m_fileSystemModel, this); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
87 m_currentDirectory->setCompleter (completer); |
13501 | 88 } |
89 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
90 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
91 FilesDockWidget::itemDoubleClicked (const QModelIndex & index) |
13501 | 92 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
93 QFileInfo fileInfo = m_fileSystemModel->fileInfo (index); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
94 if (fileInfo.isDir ()) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
95 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
96 m_fileSystemModel->setRootPath (fileInfo.absolutePath ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
97 m_fileTreeView->setRootIndex (index); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
98 setCurrentDirectory (m_fileSystemModel->fileInfo (index). |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
99 absoluteFilePath ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
100 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
101 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
102 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
103 QFileInfo fileInfo = m_fileSystemModel->fileInfo (index); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
104 emit openFile (fileInfo.filePath ()); |
13501 | 105 } |
106 } | |
107 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
108 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
109 FilesDockWidget::setCurrentDirectory (QString currentDirectory) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
110 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
111 m_currentDirectory->setText (currentDirectory); |
13501 | 112 } |
113 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
114 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
115 FilesDockWidget::onUpDirectory (void) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
116 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
117 QDir dir = |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
118 QDir (m_fileSystemModel->filePath (m_fileTreeView->rootIndex ())); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
119 dir.cdUp (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
120 m_fileSystemModel->setRootPath (dir.absolutePath ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
121 m_fileTreeView->setRootIndex (m_fileSystemModel-> |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
122 index (dir.absolutePath ())); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
123 setCurrentDirectory (dir.absolutePath ()); |
13501 | 124 } |
125 | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
126 void |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
127 FilesDockWidget::currentDirectoryEntered () |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
128 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
129 QFileInfo fileInfo (m_currentDirectory->text ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
130 if (fileInfo.isDir ()) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
131 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
132 m_fileTreeView->setRootIndex (m_fileSystemModel-> |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
133 index (fileInfo.absolutePath ())); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
134 m_fileSystemModel->setRootPath (fileInfo.absolutePath ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
135 setCurrentDirectory (fileInfo.absoluteFilePath ()); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
136 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
137 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
138 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
139 if (QFile::exists (fileInfo.absoluteFilePath ())) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13504
diff
changeset
|
140 emit openFile (fileInfo.absoluteFilePath ()); |
13501 | 141 } |
142 } |