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 <inspector/ShapeView_OpenFileDialog.hxx>
17 #include <inspector/ShapeView_OpenFileViewModel.hxx>
19 #include <Standard_WarningsDisable.hxx>
21 #include <QHBoxLayout>
22 #include <QHeaderView>
23 #include <QItemSelectionModel>
28 #include <QVBoxLayout>
31 #include <QApplication>
34 #include <QFileDialog>
35 #include <QFileSystemModel>
36 #include <QPushButton>
37 #include <QToolButton>
38 #include <Standard_WarningsRestore.hxx>
40 const int FONT_POINT_SIZE = 18;
41 const int ICON_SIZE = 40;
43 const int OPEN_DIALOG_WIDTH = 550;
44 const int OPEN_DIALOG_HEIGHT = 200;
46 const int MARGIN_DIALOG = 4;
47 const int SPACING_DIALOG = 2;
49 // =======================================================================
50 // function : StartButton
52 // =======================================================================
53 QPushButton* ShapeView_OpenButton::StartButton()
57 myStartButton = new QPushButton();
58 myStartButton->setIcon (QIcon (":/icons/folder_open.png"));
59 connect (myStartButton, SIGNAL (clicked()), this, SLOT (onStartButtonClicked()));
64 // =======================================================================
65 // function : onStartButtonClicked
67 // =======================================================================
68 void ShapeView_OpenButton::onStartButtonClicked()
70 QString aDataDirName = QDir::currentPath();
72 QString aFileName = ShapeView_OpenFileDialog::OpenFile (0, aDataDirName);
73 aFileName = QDir().toNativeSeparators (aFileName);
74 if (!aFileName.isEmpty())
76 QApplication::setOverrideCursor (Qt::WaitCursor);
77 emit OpenFile (aFileName);
78 QApplication::restoreOverrideCursor();
82 // =======================================================================
83 // function : changeMargins
85 // =======================================================================
86 void changeMargins (QBoxLayout* theLayout)
88 theLayout->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
89 theLayout->setSpacing (SPACING_DIALOG);
92 // =======================================================================
93 // function : Constructor
95 // =======================================================================
96 ShapeView_OpenFileDialog::ShapeView_OpenFileDialog (QWidget* theParent, const QString& theDataDirName)
99 setWindowTitle (theDataDirName);
100 myDataDir = theDataDirName;
102 QVBoxLayout* aDialogLay = new QVBoxLayout (this);
103 changeMargins (aDialogLay);
106 QLabel* aTitleLabel = new QLabel (this);
107 aTitleLabel->setText (tr ("Open File"));
108 aDialogLay->addWidget (aTitleLabel);
111 QGroupBox* aSamplesBox = new QGroupBox (this);
112 aSamplesBox->setTitle (tr ("Samples"));
113 aDialogLay->addWidget (aSamplesBox);
114 QVBoxLayout* aSampleLay = new QVBoxLayout (aSamplesBox);
115 changeMargins (aSampleLay);
116 mySamplesView = createTableView (readSampleNames());
117 aSampleLay->addWidget (mySamplesView);
120 QGroupBox* aSelectFileBox = new QGroupBox (this);
121 aSelectFileBox->setTitle (tr ("Select file"));
122 aDialogLay->addWidget (aSelectFileBox);
123 QGridLayout* aSelectFileLay = new QGridLayout (aSelectFileBox);
124 aSelectFileLay->setContentsMargins (MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
126 mySelectedName = new QLineEdit (aSelectFileBox);
127 QCompleter* aCompleter = new QCompleter();
128 QFileSystemModel* aFileSystemModel = new QFileSystemModel;
129 aFileSystemModel->setRootPath (QDir::rootPath());
130 aCompleter->setModel (aFileSystemModel);
131 mySelectedName->setCompleter (aCompleter);
132 aSelectFileLay->addWidget (mySelectedName, 1, 0);
134 QToolButton* aSelectFileBtn = new QToolButton (aSelectFileBox);
135 aSelectFileBtn->setIcon (QIcon (":/icons/folder_open.png"));
136 aSelectFileLay->addWidget (aSelectFileBtn, 1, 1);
138 myFolderApplyOpen = new QToolButton (aSelectFileBox);
139 myFolderApplyOpen->setIcon (QIcon (":/icons/folder_import.png"));
140 myFolderApplyOpen->setIconSize (QSize (ICON_SIZE, ICON_SIZE));
141 myFolderApplyOpen->setEnabled (false);
142 aSelectFileLay->addWidget (myFolderApplyOpen, 0, 2, 2, 1);
144 connect (mySelectedName, SIGNAL (textChanged (const QString&)),
145 this, SLOT (onNameChanged (const QString&)));
146 connect (aSelectFileBtn, SIGNAL (clicked()), this, SLOT (onSelectClicked()));
147 connect (myFolderApplyOpen, SIGNAL (clicked()), this, SLOT (onApplySelectClicked()));
149 resize (OPEN_DIALOG_WIDTH, OPEN_DIALOG_HEIGHT);
152 // =======================================================================
153 // function : OpenFile
155 // =======================================================================
156 QString ShapeView_OpenFileDialog::OpenFile (QWidget* theParent, const QString& theDataDirName)
159 ShapeView_OpenFileDialog* aDialog = new ShapeView_OpenFileDialog(theParent, theDataDirName);
160 if (aDialog->exec() == QDialog::Accepted)
161 aFileName = aDialog->GetFileName();
166 // =======================================================================
167 // function : onSampleSelectionChanged
169 // =======================================================================
170 void ShapeView_OpenFileDialog::onSampleSelectionChanged (const QItemSelection& theSelected,
171 const QItemSelection&)
173 QItemSelectionModel* aSelectionModel = (QItemSelectionModel*)sender();
174 if (!aSelectionModel)
176 if (theSelected.isEmpty())
179 QModelIndex anIndex = theSelected.first().indexes().first();
180 if (!anIndex.isValid())
183 myFileName = aSelectionModel->model()->data (anIndex, Qt::ToolTipRole).toString();
187 // =======================================================================
188 // function : onNameChanged
190 // =======================================================================
191 void ShapeView_OpenFileDialog::onNameChanged (const QString& theText)
193 QFileInfo aFileInfo (theText);
194 bool anExists = aFileInfo.exists() && aFileInfo.isFile();
195 myFolderApplyOpen->setEnabled (anExists);
198 // =======================================================================
199 // function : onSelectClicked
201 // =======================================================================
202 void ShapeView_OpenFileDialog::onSelectClicked()
204 QString anEnteredPath;
205 QString aDirName = mySelectedName->text();
206 if (!aDirName.isEmpty())
208 QDir aDir (aDirName);
210 anEnteredPath = aDirName;
213 QString aFilter (tr ("BREP file (*.brep*)"));
214 QString aFileName = QFileDialog::getOpenFileName (0, "Open document", anEnteredPath, aFilter);
216 if (aFileName.isEmpty())
217 return; // do nothing, left the previous value
219 mySelectedName->setText (aFileName);
220 onNameChanged (aFileName);
223 // =======================================================================
224 // function : onApplySelectClicked
226 // =======================================================================
227 void ShapeView_OpenFileDialog::onApplySelectClicked()
229 myFileName = mySelectedName->text();
233 // =======================================================================
234 // function : createTableView
236 // =======================================================================
237 QTableView* ShapeView_OpenFileDialog::createTableView (const QStringList& theFileNames)
239 QTableView* aTableView = new QTableView (this);
240 aTableView->setFrameStyle (QFrame::NoFrame);
241 QPalette aPalette = aTableView->viewport()->palette();
242 QColor aWindowColor = aPalette.color (QPalette::Window);
243 aPalette.setBrush (QPalette::Base, aWindowColor);
244 aTableView->viewport()->setPalette (aPalette);
246 aTableView->horizontalHeader()->setVisible (false);
247 aTableView->verticalHeader()->setVisible (false);
248 aTableView->setGridStyle (Qt::NoPen);
249 aTableView->setModel (createModel (theFileNames));
250 aTableView->setItemDelegateForRow (0, new ShapeView_OpenFileItemDelegate (aTableView,
251 aPalette.color (QPalette::Highlight)));
252 aTableView->viewport()->setAttribute (Qt::WA_Hover);
253 int aCellHeight = ICON_SIZE + aTableView->verticalHeader()->defaultSectionSize();
254 aTableView->setRowHeight (0, aCellHeight);
255 int aScrollHeight = aTableView->horizontalScrollBar()->sizeHint().height();
256 aTableView->setMinimumHeight (aCellHeight + aScrollHeight);
257 QItemSelectionModel* aSelectionModel = new QItemSelectionModel (aTableView->model());
258 connect (aSelectionModel, SIGNAL (selectionChanged (const QItemSelection&, const QItemSelection&)),
259 this, SLOT (onSampleSelectionChanged (const QItemSelection&, const QItemSelection&)));
260 aTableView->setSelectionModel (aSelectionModel);
265 // =======================================================================
266 // function : createModel
268 // =======================================================================
269 QAbstractItemModel* ShapeView_OpenFileDialog::createModel (const QStringList& theFileNames)
271 ShapeView_OpenFileViewModel* aModel = new ShapeView_OpenFileViewModel(this);
272 aModel->Init (theFileNames);
276 // =======================================================================
277 // function : readSampleNames
279 // =======================================================================
280 QStringList ShapeView_OpenFileDialog::readSampleNames()
284 QDir aDir (myDataDir);
285 aDir.setSorting(QDir::Name);
287 QFileInfoList aDirEntries = aDir.entryInfoList();
288 for (int aDirId = 0; aDirId < aDirEntries.size(); ++aDirId)
290 QFileInfo aFileInfo = aDirEntries.at (aDirId);
291 if (aFileInfo.isFile())
292 aNames.append (aFileInfo.absoluteFilePath());