1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <TInspectorEXE_OpenFileDialog.hxx>
17 #include <TInspectorEXE_OpenFileViewModel.hxx>
19 #include <inspector/TInspector_Communicator.hxx>
21 #include <Standard_WarningsDisable.hxx>
22 #include <QApplication>
25 #include <QFileDialog>
26 #include <QFileSystemModel>
27 #include <QItemSelectionModel>
29 #include <QHBoxLayout>
30 #include <QHeaderView>
35 #include <QToolButton>
36 #include <QPushButton>
37 #include <QVBoxLayout>
39 #include <Standard_WarningsRestore.hxx>
41 const int FONT_POINT_SIZE = 18;
42 const int ICON_SIZE = 40;
44 const int OPEN_DIALOG_WIDTH = 550;
45 const int OPEN_DIALOG_HEIGHT = 200;
47 const int MARGIN_DIALOG = 4;
48 const int SPACING_DIALOG = 2;
50 TInspector_Communicator* MyCommunicator;
52 // =======================================================================
53 // function : StartButton
55 // =======================================================================
56 QPushButton* TInspectorEXE_OpenButton::StartButton()
60 myStartButton = new QPushButton();
61 myStartButton->setIcon (QIcon (":folder_open.png"));
62 connect (myStartButton, SIGNAL (clicked()), this, SLOT (onStartButtonClicked()));
67 // =======================================================================
68 // function : onStartButtonClicked
70 // =======================================================================
71 void TInspectorEXE_OpenButton::onStartButtonClicked()
73 QPushButton* aButton = (QPushButton*)sender();
74 TCollection_AsciiString aPluginName (aButton->objectName().toStdString().c_str());
75 if (aPluginName.IsEmpty())
78 QString aDataDirName = QDir::currentPath();
79 if (myDefaultDirs.IsBound (aPluginName))
80 aDataDirName = myDefaultDirs.Find (aPluginName).ToCString();
82 QString aFileName = TInspectorEXE_OpenFileDialog::OpenFile (0, aDataDirName);
83 aFileName = QDir().toNativeSeparators (aFileName);
84 if (!aFileName.isEmpty()) {
85 QApplication::setOverrideCursor (Qt::WaitCursor);
86 TInspectorEXE_OpenFileDialog::Communicator()->OpenFile (aPluginName, TCollection_AsciiString (aFileName.toUtf8().data()));
87 QApplication::restoreOverrideCursor();
91 // =======================================================================
92 // function : changeMargins
94 // =======================================================================
95 void changeMargins (QBoxLayout* theLayout)
97 theLayout->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
98 theLayout->setSpacing (SPACING_DIALOG);
101 // =======================================================================
102 // function : Constructor
104 // =======================================================================
105 TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent, const QString& theDataDirName)
106 : QDialog(theParent), myDataDir (theDataDirName)
108 setWindowTitle (theDataDirName);
110 QVBoxLayout* aDialogLay = new QVBoxLayout (this);
111 changeMargins (aDialogLay);
114 QLabel* aTitleLabel = new QLabel (this);
115 aTitleLabel->setText (tr ("Open File"));
116 aDialogLay->addWidget (aTitleLabel);
119 QGroupBox* aSamplesBox = new QGroupBox (this);
120 aSamplesBox->setTitle (tr ("Samples"));
121 aDialogLay->addWidget (aSamplesBox);
122 QVBoxLayout* aSampleLay = new QVBoxLayout (aSamplesBox);
123 changeMargins (aSampleLay);
124 mySamplesView = createTableView (readSampleNames());
125 aSampleLay->addWidget (mySamplesView);
128 QGroupBox* aSelectFileBox = new QGroupBox (this);
129 aSelectFileBox->setTitle (tr ("Select file"));
130 aDialogLay->addWidget (aSelectFileBox);
131 QGridLayout* aSelectFileLay = new QGridLayout (aSelectFileBox);
132 aSelectFileLay->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
134 mySelectedName = new QLineEdit (aSelectFileBox);
135 QCompleter* aCompleter = new QCompleter();
136 QFileSystemModel* aFileSystemModel = new QFileSystemModel;
137 aFileSystemModel->setRootPath (QDir::rootPath());
138 aCompleter->setModel (aFileSystemModel);
139 mySelectedName->setCompleter (aCompleter);
140 aSelectFileLay->addWidget (mySelectedName, 1, 0);
142 QToolButton* aSelectFileBtn = new QToolButton (aSelectFileBox);
143 aSelectFileBtn->setIcon (QIcon (":folder_open.png"));
144 aSelectFileLay->addWidget (aSelectFileBtn, 1, 1);
146 myFolderApplyOpen = new QToolButton (aSelectFileBox);
147 myFolderApplyOpen->setIcon (QIcon (":folder_import.png"));
148 myFolderApplyOpen->setIconSize (QSize (ICON_SIZE, ICON_SIZE));
149 myFolderApplyOpen->setEnabled (false);
150 aSelectFileLay->addWidget (myFolderApplyOpen, 0, 2, 2, 1);
152 connect (mySelectedName, SIGNAL (textChanged (const QString&)),
153 this, SLOT (onNameChanged (const QString&)));
154 connect (aSelectFileBtn, SIGNAL (clicked()), this, SLOT (onSelectClicked()));
155 connect (myFolderApplyOpen, SIGNAL (clicked()), this, SLOT (onApplySelectClicked()));
157 resize (OPEN_DIALOG_WIDTH, OPEN_DIALOG_HEIGHT);
160 // =======================================================================
161 // function : OpenFile
163 // =======================================================================
164 QString TInspectorEXE_OpenFileDialog::OpenFile (QWidget* theParent, const QString& theDataDirName)
167 TInspectorEXE_OpenFileDialog* aDialog = new TInspectorEXE_OpenFileDialog(theParent, theDataDirName);
168 if (aDialog->exec() == QDialog::Accepted)
169 aFileName = aDialog->GetFileName();
174 // =======================================================================
175 // function : Communicator
177 // =======================================================================
178 TInspector_Communicator* TInspectorEXE_OpenFileDialog::Communicator()
181 MyCommunicator = new TInspector_Communicator();
182 return MyCommunicator;
185 // =======================================================================
186 // function : onSampleSelectionChanged
188 // =======================================================================
189 void TInspectorEXE_OpenFileDialog::onSampleSelectionChanged (const QItemSelection& theSelected,
190 const QItemSelection&)
192 QItemSelectionModel* aSelectionModel = (QItemSelectionModel*)sender();
193 if (!aSelectionModel)
195 if (theSelected.isEmpty())
198 QModelIndex anIndex = theSelected.first().indexes().first();
199 if (!anIndex.isValid())
202 myFileName = aSelectionModel->model()->data (anIndex, Qt::ToolTipRole).toString();
206 // =======================================================================
207 // function : onNameChanged
209 // =======================================================================
210 void TInspectorEXE_OpenFileDialog::onNameChanged (const QString& theText)
212 QFileInfo aFileInfo (theText);
213 bool anExists = aFileInfo.exists() && aFileInfo.isFile();
214 myFolderApplyOpen->setEnabled (anExists);
217 // =======================================================================
218 // function : onSelectClicked
220 // =======================================================================
221 void TInspectorEXE_OpenFileDialog::onSelectClicked()
223 QString anEnteredPath;
224 QString aDirName = mySelectedName->text();
225 if (!aDirName.isEmpty())
227 QDir aDir (aDirName);
229 anEnteredPath = aDirName;
232 QString aFileName = QFileDialog::getOpenFileName (0, "Open document", anEnteredPath);
234 if (aFileName.isEmpty())
235 return; // do nothing, left the previous value
238 myFileName = aFileName;
242 // =======================================================================
243 // function : onApplySelectClicked
245 // =======================================================================
246 void TInspectorEXE_OpenFileDialog::onApplySelectClicked()
248 myFileName = mySelectedName->text();
252 // =======================================================================
253 // function : createTableView
255 // =======================================================================
256 QTableView* TInspectorEXE_OpenFileDialog::createTableView (const QStringList& theFileNames)
258 QTableView* aTableView = new QTableView (this);
259 aTableView->setFrameStyle (QFrame::NoFrame);
260 QPalette aPalette = aTableView->viewport()->palette();
261 QColor aWindowColor = aPalette.color (QPalette::Window);
262 aPalette.setBrush (QPalette::Base, aWindowColor);
263 aTableView->viewport()->setPalette (aPalette);
265 aTableView->horizontalHeader()->setVisible (false);
266 aTableView->verticalHeader()->setVisible (false);
267 aTableView->setGridStyle (Qt::NoPen);
268 aTableView->setModel (createModel (theFileNames));
269 aTableView->setItemDelegateForRow (0, new TInspectorEXE_OpenFileItemDelegate (aTableView,
270 aPalette.color (QPalette::Highlight)));
271 aTableView->viewport()->setAttribute (Qt::WA_Hover);
272 int aCellHeight = ICON_SIZE + aTableView->verticalHeader()->defaultSectionSize();
273 aTableView->setRowHeight (0, aCellHeight);
274 int aScrollHeight = aTableView->horizontalScrollBar()->sizeHint().height();
275 aTableView->setMinimumHeight (aCellHeight + aScrollHeight);
276 QItemSelectionModel* aSelectionModel = new QItemSelectionModel (aTableView->model());
277 connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
278 this, SLOT (onSampleSelectionChanged (const QItemSelection&, const QItemSelection&)));
279 aTableView->setSelectionModel (aSelectionModel);
284 // =======================================================================
285 // function : createModel
287 // =======================================================================
288 QAbstractItemModel* TInspectorEXE_OpenFileDialog::createModel (const QStringList& theFileNames)
290 TInspectorEXE_OpenFileViewModel* aModel = new TInspectorEXE_OpenFileViewModel (this);
291 aModel->Init (theFileNames);
295 // =======================================================================
296 // function : readSampleNames
298 // =======================================================================
299 QStringList TInspectorEXE_OpenFileDialog::readSampleNames()
303 QDir aDir(myDataDir);
304 aDir.setSorting(QDir::Name);
306 QFileInfoList aDirEntries = aDir.entryInfoList();
307 for (int aDirId = 0; aDirId < aDirEntries.size(); ++aDirId)
309 QFileInfo aFileInfo = aDirEntries.at(aDirId);
310 if (aFileInfo.isFile())
311 aNames.append (aFileInfo.absoluteFilePath());