File: ShapeView_OpenFileDialog.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,932 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 31; javascript: 22
file content (300 lines) | stat: -rw-r--r-- 10,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Created on: 2017-06-16
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2017 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <inspector/ShapeView_OpenFileDialog.hxx>
#include <inspector/ShapeView_OpenFileViewModel.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QItemSelectionModel>
#include <QLabel>
#include <QLineEdit>
#include <QScrollBar>
#include <QTableView>
#include <QVBoxLayout>
#include <QWidget>

#include <QApplication>
#include <QCompleter>
#include <QDir>
#include <QFileDialog>
#include <QFileSystemModel>
#include <QPushButton>
#include <QToolButton>
#include <Standard_WarningsRestore.hxx>

static const int ICON_SIZE = 40;

static const int OPEN_DIALOG_WIDTH  = 550;
static const int OPEN_DIALOG_HEIGHT = 200;

static const int MARGIN_DIALOG  = 4;
static const int SPACING_DIALOG = 2;

// =======================================================================
// function : StartButton
// purpose :
// =======================================================================
QPushButton* ShapeView_OpenButton::StartButton()
{
  if (!myStartButton)
  {
    myStartButton = new QPushButton();
    myStartButton->setIcon(QIcon(":/icons/folder_open.png"));
    connect(myStartButton, SIGNAL(clicked()), this, SLOT(onStartButtonClicked()));
  }
  return myStartButton;
}

// =======================================================================
// function : onStartButtonClicked
// purpose :
// =======================================================================
void ShapeView_OpenButton::onStartButtonClicked()
{
  QString aDataDirName = QDir::currentPath();

  QString aFileName = ShapeView_OpenFileDialog::OpenFile(0, aDataDirName);
  aFileName         = QDir().toNativeSeparators(aFileName);
  if (!aFileName.isEmpty())
  {
    QApplication::setOverrideCursor(Qt::WaitCursor);
    emit OpenFile(aFileName);
    QApplication::restoreOverrideCursor();
  }
}

// =======================================================================
// function : changeMargins
// purpose :
// =======================================================================
void changeMargins(QBoxLayout* theLayout)
{
  theLayout->setContentsMargins(MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);
  theLayout->setSpacing(SPACING_DIALOG);
}

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
ShapeView_OpenFileDialog::ShapeView_OpenFileDialog(QWidget*       theParent,
                                                   const QString& theDataDirName)
    : QDialog(theParent)
{
  setWindowTitle(theDataDirName);
  myDataDir = theDataDirName;

  QVBoxLayout* aDialogLay = new QVBoxLayout(this);
  changeMargins(aDialogLay);

  // Title label
  QLabel* aTitleLabel = new QLabel(this);
  aTitleLabel->setText(tr("Open File"));
  aDialogLay->addWidget(aTitleLabel);

  // Samples View
  QGroupBox* aSamplesBox = new QGroupBox(this);
  aSamplesBox->setTitle(tr("Samples"));
  aDialogLay->addWidget(aSamplesBox);
  QVBoxLayout* aSampleLay = new QVBoxLayout(aSamplesBox);
  changeMargins(aSampleLay);
  mySamplesView = createTableView(readSampleNames());
  aSampleLay->addWidget(mySamplesView);

  // Select file
  QGroupBox* aSelectFileBox = new QGroupBox(this);
  aSelectFileBox->setTitle(tr("Select file"));
  aDialogLay->addWidget(aSelectFileBox);
  QGridLayout* aSelectFileLay = new QGridLayout(aSelectFileBox);
  aSelectFileLay->setContentsMargins(MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG, MARGIN_DIALOG);

  mySelectedName                     = new QLineEdit(aSelectFileBox);
  QCompleter*       aCompleter       = new QCompleter();
  QFileSystemModel* aFileSystemModel = new QFileSystemModel;
  aFileSystemModel->setRootPath(QDir::rootPath());
  aCompleter->setModel(aFileSystemModel);
  mySelectedName->setCompleter(aCompleter);
  aSelectFileLay->addWidget(mySelectedName, 1, 0);

  QToolButton* aSelectFileBtn = new QToolButton(aSelectFileBox);
  aSelectFileBtn->setIcon(QIcon(":/icons/folder_open.png"));
  aSelectFileLay->addWidget(aSelectFileBtn, 1, 1);

  myFolderApplyOpen = new QToolButton(aSelectFileBox);
  myFolderApplyOpen->setIcon(QIcon(":/icons/folder_import.png"));
  myFolderApplyOpen->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
  myFolderApplyOpen->setEnabled(false);
  aSelectFileLay->addWidget(myFolderApplyOpen, 0, 2, 2, 1);

  connect(mySelectedName,
          SIGNAL(textChanged(const QString&)),
          this,
          SLOT(onNameChanged(const QString&)));
  connect(aSelectFileBtn, SIGNAL(clicked()), this, SLOT(onSelectClicked()));
  connect(myFolderApplyOpen, SIGNAL(clicked()), this, SLOT(onApplySelectClicked()));

  resize(OPEN_DIALOG_WIDTH, OPEN_DIALOG_HEIGHT);
}

// =======================================================================
// function : OpenFile
// purpose :
// =======================================================================
QString ShapeView_OpenFileDialog::OpenFile(QWidget* theParent, const QString& theDataDirName)
{
  QString                   aFileName;
  ShapeView_OpenFileDialog* aDialog = new ShapeView_OpenFileDialog(theParent, theDataDirName);
  if (aDialog->exec() == QDialog::Accepted)
    aFileName = aDialog->GetFileName();

  return aFileName;
}

// =======================================================================
// function : onSampleSelectionChanged
// purpose :
// =======================================================================
void ShapeView_OpenFileDialog::onSampleSelectionChanged(const QItemSelection& theSelected,
                                                        const QItemSelection&)
{
  QItemSelectionModel* aSelectionModel = (QItemSelectionModel*)sender();
  if (!aSelectionModel)
    return;
  if (theSelected.isEmpty())
    return;

  QModelIndex anIndex = theSelected.first().indexes().first();
  if (!anIndex.isValid())
    return;

  myFileName = aSelectionModel->model()->data(anIndex, Qt::ToolTipRole).toString();
  accept();
}

// =======================================================================
// function : onNameChanged
// purpose :
// =======================================================================
void ShapeView_OpenFileDialog::onNameChanged(const QString& theText)
{
  QFileInfo aFileInfo(theText);
  bool      anExists = aFileInfo.exists() && aFileInfo.isFile();
  myFolderApplyOpen->setEnabled(anExists);
}

// =======================================================================
// function : onSelectClicked
// purpose :
// =======================================================================
void ShapeView_OpenFileDialog::onSelectClicked()
{
  QString anEnteredPath;
  QString aDirName = mySelectedName->text();
  if (!aDirName.isEmpty())
  {
    QDir aDir(aDirName);
    if (aDir.exists())
      anEnteredPath = aDirName;
  }

  QString aFilter(tr("BREP file (*.brep*)"));
  QString aFileName = QFileDialog::getOpenFileName(0, "Open document", anEnteredPath, aFilter);

  if (aFileName.isEmpty())
    return; // do nothing, left the previous value

  mySelectedName->setText(aFileName);
  onNameChanged(aFileName);
}

// =======================================================================
// function : onApplySelectClicked
// purpose :
// =======================================================================
void ShapeView_OpenFileDialog::onApplySelectClicked()
{
  myFileName = mySelectedName->text();
  accept();
}

// =======================================================================
// function : createTableView
// purpose :
// =======================================================================
QTableView* ShapeView_OpenFileDialog::createTableView(const QStringList& theFileNames)
{
  QTableView* aTableView = new QTableView(this);
  aTableView->setFrameStyle(QFrame::NoFrame);
  QPalette aPalette     = aTableView->viewport()->palette();
  QColor   aWindowColor = aPalette.color(QPalette::Window);
  aPalette.setBrush(QPalette::Base, aWindowColor);
  aTableView->viewport()->setPalette(aPalette);

  aTableView->horizontalHeader()->setVisible(false);
  aTableView->verticalHeader()->setVisible(false);
  aTableView->setGridStyle(Qt::NoPen);
  aTableView->setModel(createModel(theFileNames));
  aTableView->setItemDelegateForRow(
    0,
    new ShapeView_OpenFileItemDelegate(aTableView, aPalette.color(QPalette::Highlight)));
  aTableView->viewport()->setAttribute(Qt::WA_Hover);
  int aCellHeight = ICON_SIZE + aTableView->verticalHeader()->defaultSectionSize();
  aTableView->setRowHeight(0, aCellHeight);
  int aScrollHeight = aTableView->horizontalScrollBar()->sizeHint().height();
  aTableView->setMinimumHeight(aCellHeight + aScrollHeight);
  QItemSelectionModel* aSelectionModel = new QItemSelectionModel(aTableView->model());
  connect(aSelectionModel,
          SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
          this,
          SLOT(onSampleSelectionChanged(const QItemSelection&, const QItemSelection&)));
  aTableView->setSelectionModel(aSelectionModel);

  return aTableView;
}

// =======================================================================
// function : createModel
// purpose :
// =======================================================================
QAbstractItemModel* ShapeView_OpenFileDialog::createModel(const QStringList& theFileNames)
{
  ShapeView_OpenFileViewModel* aModel = new ShapeView_OpenFileViewModel(this);
  aModel->Init(theFileNames);
  return aModel;
}

// =======================================================================
// function : readSampleNames
// purpose :
// =======================================================================
QStringList ShapeView_OpenFileDialog::readSampleNames()
{
  QStringList aNames;

  QDir aDir(myDataDir);
  aDir.setSorting(QDir::Name);

  QFileInfoList aDirEntries = aDir.entryInfoList();
  for (int aDirId = 0; aDirId < aDirEntries.size(); ++aDirId)
  {
    QFileInfo aFileInfo = aDirEntries.at(aDirId);
    if (aFileInfo.isFile())
      aNames.append(aFileInfo.absoluteFilePath());
  }
  return aNames;
}