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 <QApplication>
24 #include <QFileDialog>
25 #include <QFileSystemModel>
26 #include <QItemSelectionModel>
28 #include <QHBoxLayout>
29 #include <QHeaderView>
34 #include <QToolButton>
35 #include <QPushButton>
36 #include <QVBoxLayout>
39 const int FONT_POINT_SIZE = 18;
40 const int ICON_SIZE = 40;
42 const int OPEN_DIALOG_WIDTH = 550;
43 const int OPEN_DIALOG_HEIGHT = 200;
45 const int MARGIN_DIALOG = 4;
46 const int SPACING_DIALOG = 2;
48 TInspector_Communicator* MyCommunicator;
50 // =======================================================================
51 // function : StartButton
53 // =======================================================================
54 QPushButton* TInspectorEXE_OpenButton::StartButton()
58 myStartButton = new QPushButton();
59 myStartButton->setIcon (QIcon (":/icons/folder_open.png"));
60 connect (myStartButton, SIGNAL (clicked()), this, SLOT (onStartButtonClicked()));
65 // =======================================================================
66 // function : onStartButtonClicked
68 // =======================================================================
69 void TInspectorEXE_OpenButton::onStartButtonClicked()
71 QPushButton* aButton = (QPushButton*)sender();
72 TCollection_AsciiString aPluginName (aButton->objectName().toStdString().c_str());
73 if (aPluginName.IsEmpty())
76 QString aDataDirName = QDir::currentPath();
77 if (myDefaultDirs.IsBound (aPluginName))
78 aDataDirName = myDefaultDirs.Find (aPluginName).ToCString();
80 QString aFileName = TInspectorEXE_OpenFileDialog::OpenFile (0, aDataDirName);
81 aFileName = QDir().toNativeSeparators (aFileName);
82 if (!aFileName.isEmpty()) {
83 QApplication::setOverrideCursor (Qt::WaitCursor);
84 TInspectorEXE_OpenFileDialog::Communicator()->OpenFile (aPluginName, TCollection_AsciiString (aFileName.toUtf8().data()));
85 QApplication::restoreOverrideCursor();
89 // =======================================================================
90 // function : changeMargins
92 // =======================================================================
93 void changeMargins (QBoxLayout* theLayout)
95 theLayout->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
96 theLayout->setSpacing (SPACING_DIALOG);
99 // =======================================================================
100 // function : Constructor
102 // =======================================================================
103 TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent, const QString& theDataDirName)
104 : QDialog(theParent), myDataDir (theDataDirName)
106 setWindowTitle (theDataDirName);
108 QVBoxLayout* aDialogLay = new QVBoxLayout (this);
109 changeMargins (aDialogLay);
112 QLabel* aTitleLabel = new QLabel (this);
113 aTitleLabel->setText (tr ("Open File"));
114 aDialogLay->addWidget (aTitleLabel);
117 QGroupBox* aSamplesBox = new QGroupBox (this);
118 aSamplesBox->setTitle (tr ("Samples"));
119 aDialogLay->addWidget (aSamplesBox);
120 QVBoxLayout* aSampleLay = new QVBoxLayout (aSamplesBox);
121 changeMargins (aSampleLay);
122 mySamplesView = createTableView (readSampleNames());
123 aSampleLay->addWidget (mySamplesView);
126 QGroupBox* aSelectFileBox = new QGroupBox (this);
127 aSelectFileBox->setTitle (tr ("Select file"));
128 aDialogLay->addWidget (aSelectFileBox);
129 QGridLayout* aSelectFileLay = new QGridLayout (aSelectFileBox);
130 aSelectFileLay->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
132 mySelectedName = new QLineEdit (aSelectFileBox);
133 QCompleter* aCompleter = new QCompleter();
134 QFileSystemModel* aFileSystemModel = new QFileSystemModel;
135 aFileSystemModel->setRootPath (QDir::rootPath());
136 aCompleter->setModel (aFileSystemModel);
137 mySelectedName->setCompleter (aCompleter);
138 aSelectFileLay->addWidget (mySelectedName, 1, 0);
140 QToolButton* aSelectFileBtn = new QToolButton (aSelectFileBox);
141 aSelectFileBtn->setIcon (QIcon (":/icons/folder_open.png"));
142 aSelectFileLay->addWidget (aSelectFileBtn, 1, 1);
144 myFolderApplyOpen = new QToolButton (aSelectFileBox);
145 myFolderApplyOpen->setIcon (QIcon (":/icons/folder_import.png"));
146 myFolderApplyOpen->setIconSize (QSize (ICON_SIZE, ICON_SIZE));
147 myFolderApplyOpen->setEnabled (false);
148 aSelectFileLay->addWidget (myFolderApplyOpen, 0, 2, 2, 1);
150 connect (mySelectedName, SIGNAL (textChanged (const QString&)),
151 this, SLOT (onNameChanged (const QString&)));
152 connect (aSelectFileBtn, SIGNAL (clicked()), this, SLOT (onSelectClicked()));
153 connect (myFolderApplyOpen, SIGNAL (clicked()), this, SLOT (onApplySelectClicked()));
155 resize (OPEN_DIALOG_WIDTH, OPEN_DIALOG_HEIGHT);
158 // =======================================================================
159 // function : OpenFile
161 // =======================================================================
162 QString TInspectorEXE_OpenFileDialog::OpenFile (QWidget* theParent, const QString& theDataDirName)
165 TInspectorEXE_OpenFileDialog* aDialog = new TInspectorEXE_OpenFileDialog(theParent, theDataDirName);
166 if (aDialog->exec() == QDialog::Accepted)
167 aFileName = aDialog->GetFileName();
172 // =======================================================================
173 // function : Communicator
175 // =======================================================================
176 TInspector_Communicator* TInspectorEXE_OpenFileDialog::Communicator()
179 MyCommunicator = new TInspector_Communicator();
180 return MyCommunicator;
183 // =======================================================================
184 // function : onSampleSelectionChanged
186 // =======================================================================
187 void TInspectorEXE_OpenFileDialog::onSampleSelectionChanged (const QItemSelection& theSelected,
188 const QItemSelection&)
190 QItemSelectionModel* aSelectionModel = (QItemSelectionModel*)sender();
191 if (!aSelectionModel)
193 if (theSelected.isEmpty())
196 QModelIndex anIndex = theSelected.first().indexes().first();
197 if (!anIndex.isValid())
200 myFileName = aSelectionModel->model()->data (anIndex, Qt::ToolTipRole).toString();
204 // =======================================================================
205 // function : onNameChanged
207 // =======================================================================
208 void TInspectorEXE_OpenFileDialog::onNameChanged (const QString& theText)
210 QFileInfo aFileInfo (theText);
211 bool anExists = aFileInfo.exists() && aFileInfo.isFile();
212 myFolderApplyOpen->setEnabled (anExists);
215 // =======================================================================
216 // function : onSelectClicked
218 // =======================================================================
219 void TInspectorEXE_OpenFileDialog::onSelectClicked()
221 QString anEnteredPath;
222 QString aDirName = mySelectedName->text();
223 if (!aDirName.isEmpty())
225 QDir aDir (aDirName);
227 anEnteredPath = aDirName;
230 QString aFileName = QFileDialog::getOpenFileName (0, "Open document", anEnteredPath);
232 if (aFileName.isEmpty())
233 return; // do nothing, left the previous value
235 mySelectedName->setText (aFileName);
236 onNameChanged (aFileName);
239 // =======================================================================
240 // function : onApplySelectClicked
242 // =======================================================================
243 void TInspectorEXE_OpenFileDialog::onApplySelectClicked()
245 myFileName = mySelectedName->text();
249 // =======================================================================
250 // function : createTableView
252 // =======================================================================
253 QTableView* TInspectorEXE_OpenFileDialog::createTableView (const QStringList& theFileNames)
255 QTableView* aTableView = new QTableView (this);
256 aTableView->setFrameStyle (QFrame::NoFrame);
257 QPalette aPalette = aTableView->viewport()->palette();
258 QColor aWindowColor = aPalette.color (QPalette::Window);
259 aPalette.setBrush (QPalette::Base, aWindowColor);
260 aTableView->viewport()->setPalette (aPalette);
262 aTableView->horizontalHeader()->setVisible (false);
263 aTableView->verticalHeader()->setVisible (false);
264 aTableView->setGridStyle (Qt::NoPen);
265 aTableView->setModel (createModel (theFileNames));
266 aTableView->setItemDelegateForRow (0, new TInspectorEXE_OpenFileItemDelegate (aTableView,
267 aPalette.color (QPalette::Highlight)));
268 aTableView->viewport()->setAttribute (Qt::WA_Hover);
269 int aCellHeight = ICON_SIZE + aTableView->verticalHeader()->defaultSectionSize();
270 aTableView->setRowHeight (0, aCellHeight);
271 int aScrollHeight = aTableView->horizontalScrollBar()->sizeHint().height();
272 aTableView->setMinimumHeight (aCellHeight + aScrollHeight);
273 QItemSelectionModel* aSelectionModel = new QItemSelectionModel (aTableView->model());
274 connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
275 this, SLOT (onSampleSelectionChanged (const QItemSelection&, const QItemSelection&)));
276 aTableView->setSelectionModel (aSelectionModel);
281 // =======================================================================
282 // function : createModel
284 // =======================================================================
285 QAbstractItemModel* TInspectorEXE_OpenFileDialog::createModel (const QStringList& theFileNames)
287 TInspectorEXE_OpenFileViewModel* aModel = new TInspectorEXE_OpenFileViewModel (this);
288 aModel->Init (theFileNames);
292 // =======================================================================
293 // function : readSampleNames
295 // =======================================================================
296 QStringList TInspectorEXE_OpenFileDialog::readSampleNames()
300 QDir aDir(myDataDir);
301 aDir.setSorting(QDir::Name);
303 QFileInfoList aDirEntries = aDir.entryInfoList();
304 for (int aDirId = 0; aDirId < aDirEntries.size(); ++aDirId)
306 QFileInfo aFileInfo = aDirEntries.at(aDirId);
307 if (aFileInfo.isFile())
308 aNames.append (aFileInfo.absoluteFilePath());