0030268: Inspectors - improvements in VInspector plugin
[occt.git] / tools / TInspector / TInspector_OpenButton.cxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #include <inspector/TInspector_OpenButton.hxx>
17
18 #include <inspector/TInspector_Communicator.hxx>
19 #include <inspector/TInspector_OpenFileDialog.hxx>
20
21 #include <Standard_WarningsDisable.hxx>
22 #include <QApplication>
23 #include <QDir>
24 #include <QPushButton>
25 #include <Standard_WarningsRestore.hxx>
26
27 const int RECENT_FILES_CACHE_SIZE = 10;
28
29 // =======================================================================
30 // function : Constructor
31 // purpose :
32 // =======================================================================
33 TInspector_OpenButton::TInspector_OpenButton (QObject* theParent)
34  : QObject (theParent), myStartButton (0)
35 {
36 }
37
38 // =======================================================================
39 // function : StartButton
40 // purpose :
41 // =======================================================================
42 QPushButton* TInspector_OpenButton::StartButton()
43 {
44   if (!myStartButton)
45   {
46     myStartButton = new QPushButton();
47     myStartButton->setIcon (QIcon (":folder_open.png"));
48     connect (myStartButton, SIGNAL (clicked()), this, SLOT (onStartButtonClicked()));
49   }
50   return myStartButton;
51 }
52
53 // =======================================================================
54 // function : onStartButtonClicked
55 // purpose :
56 // =======================================================================
57 void TInspector_OpenButton::onStartButtonClicked()
58 {
59   QPushButton* aButton = (QPushButton*)sender();
60   TCollection_AsciiString aPluginName (aButton->objectName().toStdString().c_str());
61   if (aPluginName.IsEmpty())
62     return;
63   
64   QStringList aPluginRecentlyOpenedFiles;
65   if (myRecentlyOpenedFiles.contains(aPluginName))
66   {
67     QStringList aFileNames = myRecentlyOpenedFiles[aPluginName];
68     for (int i = 0; i < aFileNames.size(); i++)
69     {
70       QFileInfo aFileInfo (aFileNames[i]);
71       if (aFileInfo.exists() && aFileInfo.isFile())
72         aPluginRecentlyOpenedFiles.append(aFileInfo.absoluteFilePath());
73     }
74   }
75
76   QString aFileName = TInspector_OpenFileDialog::OpenFile (0, aPluginRecentlyOpenedFiles);
77   aFileName = QDir().toNativeSeparators (aFileName);
78   if (!aFileName.isEmpty()) {
79     QApplication::setOverrideCursor (Qt::WaitCursor);
80     TInspector_OpenFileDialog::Communicator()->OpenFile (aPluginName, TCollection_AsciiString (aFileName.toUtf8().data()));
81
82     QFileInfo aFileInfo (aFileName);
83     if (!aPluginRecentlyOpenedFiles.contains (aFileInfo.absoluteFilePath()))
84     {
85       myRecentlyOpenedFiles[aPluginName].append (aFileInfo.absoluteFilePath());
86       for (int i = 0; i < myRecentlyOpenedFiles[aPluginName].size() - RECENT_FILES_CACHE_SIZE; i++)
87         myRecentlyOpenedFiles[aPluginName].removeFirst();
88       TInspector_OpenFileDialog::SetPluginRecentlyOpenedFiles (aPluginName,
89         TInspector_OpenFileDialog::Communicator(), myRecentlyOpenedFiles[aPluginName]);
90
91       TInspector_OpenFileDialog::Communicator()->GetPluginParameters()->StorePreferences();
92     }
93
94     QApplication::restoreOverrideCursor();
95   }
96 }