Skip to content

Commit

Permalink
0029748: Samples - Inspector tool - use recently opened files in TIns…
Browse files Browse the repository at this point in the history
…pectorEXE

- recently files using in open file dialog
- correction of TInspectorEXE title to show opened file path by Start
  • Loading branch information
nds authored and a-betenev committed May 23, 2018
1 parent d0fbc7e commit 042e09a
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 100 deletions.
6 changes: 5 additions & 1 deletion tools/DFBrowser/DFBrowser_Window.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ void DFBrowser_Window::SetParent (void* theParent)
QLayout* aLayout = myParent->layout();
if (aLayout)
aLayout->addWidget (GetMainWindow());

if (!myOpenedFileName.isEmpty())
myParent->setObjectName(myOpenedFileName);
}
}

Expand All @@ -247,7 +250,6 @@ void DFBrowser_Window::FillActionsMenu (void* theMenu)
// =======================================================================
void DFBrowser_Window::GetPreferences (TInspectorAPI_PreferencesDataMap& theItem)
{
theItem.Clear();
theItem.Bind ("geometry", TreeModel_Tools::ToString (myMainWindow->saveState()).toStdString().c_str());

QMap<QString, QString> anItems;
Expand Down Expand Up @@ -498,6 +500,8 @@ void DFBrowser_Window::OpenFile (const TCollection_AsciiString& theFileName)

if (myParent)
myParent->setObjectName (isSTEPFileName ? QString (TCollection_AsciiString (theFileName).ToCString()) : getWindowTitle());
else
myOpenedFileName = isSTEPFileName ? QString(TCollection_AsciiString(theFileName).ToCString()) : getWindowTitle();

if (anApplication.IsNull())
{
Expand Down
1 change: 1 addition & 0 deletions tools/DFBrowser/DFBrowser_Window.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private:
DFBrowser_Thread* myThread; //!< Threads manipulator, starting thread items, listens finalizing
ViewControl_MessageDialog* myExportToShapeViewDialog; //!< dialog about exporting TopoDS_Shape to ShapeView plugin
Handle(TInspectorAPI_PluginParameters) myParameters; //!< contains application, context, files that should be opened
QString myOpenedFileName; //!< cached name of opened file between parent is set, apply it by parent setting and nullify
};

#endif
1 change: 0 additions & 1 deletion tools/ShapeView/ShapeView_Window.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ void ShapeView_Window::FillActionsMenu (void* theMenu)
// =======================================================================
void ShapeView_Window::GetPreferences (TInspectorAPI_PreferencesDataMap& theItem)
{
theItem.Clear();
theItem.Bind ("geometry", TreeModel_Tools::ToString (myMainWindow->saveState()).toStdString().c_str());

QMap<QString, QString> anItems;
Expand Down
5 changes: 4 additions & 1 deletion tools/TInspector/TInspector_Communicator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define TInspector_Communicator_H

#include <inspector/TInspector_Window.hxx>
#include <inspector/TInspectorAPI_PluginParameters.hxx>

#include <NCollection_List.hxx>
#include <Standard.hxx>
Expand Down Expand Up @@ -107,8 +108,10 @@ public:
//! \param theStream stream for output
void Dump (Standard_OStream& theStream) const { return myWindow->Dump (theStream); }

private:
//! Returns plugins parameters container
Handle(TInspectorAPI_PluginParameters) const GetPluginParameters() { return myWindow->GetPluginParameters(); }

private:
TInspector_Window* myWindow; //!< current window
};

Expand Down
2 changes: 1 addition & 1 deletion tools/TInspector/TInspector_PluginParameters.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public:
{ myPreferences->SetPreferences (thePluginName, theItem); }

//! Store plugin preferences into a preferences file
void StorePreferences() { myPreferences->StorePreferences(); }
virtual void StorePreferences() Standard_OVERRIDE { myPreferences->StorePreferences(); }

//! Remove plugin preferences file
void RemovePreferences() { myPreferences->RemovePreferences(); }
Expand Down
1 change: 0 additions & 1 deletion tools/TInspector/TInspector_Preferences.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
void TInspector_Preferences::GetPreferences (const TCollection_AsciiString& thePluginName,
TInspectorAPI_PreferencesDataMap& theItem)
{
theItem.Clear();
if (!myIsLoadedPreferences)
{
loadPreferences();
Expand Down
4 changes: 2 additions & 2 deletions tools/TInspector/TInspector_Window.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ bool TInspector_Window::LoadPlugin (const TCollection_AsciiString& thePluginName
// =======================================================================
void TInspector_Window::GetPreferences (TInspectorAPI_PreferencesDataMap& theItem)
{
theItem.Clear();
theItem.Bind ("geometry", TreeModel_Tools::ToString (myMainWindow->saveGeometry()).toStdString().c_str());
theItem.Bind ("geometry", TreeModel_Tools::ToString (myMainWindow->saveGeometry()).toStdString().c_str());
}

// =======================================================================
Expand Down Expand Up @@ -414,6 +413,7 @@ void TInspector_Window::OnStorePreferences()
if (!anInfo.myCommunicator)
continue;

aParameters->GetPreferences (anInfo.myName, aPreferences);
anInfo.myCommunicator->GetPreferences (aPreferences);
myParameters->SetPreferences (anInfo.myName, aPreferences);
}
Expand Down
4 changes: 4 additions & 0 deletions tools/TInspector/TInspector_Window.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public:
//! \return path
TCollection_AsciiString GetTemporaryDirectory() const { return myParameters->GetTemporaryDirectory(); }

//! Returns plugins parameters container
//! \return instance of parameters container
Handle(TInspectorAPI_PluginParameters) GetPluginParameters() const { return myParameters; }

//! Returns plugin preferences: dock widgets state, tree view columns.
//! \param theItem container of preference elements
Standard_EXPORT void GetPreferences (TInspectorAPI_PreferencesDataMap& theItem);
Expand Down
3 changes: 3 additions & 0 deletions tools/TInspectorAPI/TInspectorAPI_PluginParameters.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public:
Standard_EXPORT virtual void SetPreferences (const TCollection_AsciiString& thePluginName,
const TInspectorAPI_PreferencesDataMap& theItem) = 0;

//! Store plugin preferences into a preferences file
Standard_EXPORT virtual void StorePreferences() = 0;

//! Converts a Shape parameters excepting TShape into a string value
//! \param theShape processed shape
//! \return string instance
Expand Down
45 changes: 27 additions & 18 deletions tools/TInspectorEXE/TInspectorEXE.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
// function : fileNameInDataDir
// purpose :
// =======================================================================
TCollection_AsciiString fileNameInDataDir(const TCollection_AsciiString& theEnvironmentDir,
const TCollection_AsciiString& theName)
TCollection_AsciiString fileNameInDataDir (const TCollection_AsciiString& theEnvironmentDir,
const TCollection_AsciiString& theName)
{
OSD_Environment anEnvironment(theEnvironmentDir);
OSD_Environment anEnvironment (theEnvironmentDir);

TCollection_AsciiString aFileName = anEnvironment.Value();
aFileName += TCollection_AsciiString("/") + theName;
aFileName += TCollection_AsciiString ("/") + theName;

return aFileName;
}
Expand All @@ -52,22 +52,31 @@ TCollection_AsciiString fileNameInDataDir(const TCollection_AsciiString& theEnvi
void setPluginSampleDirectory (const TCollection_AsciiString& theName, TInspector_Communicator* theCommunicator,
TInspectorEXE_OpenButton* theButtonControl)
{
if (theName.IsEqual ("TKDFBrowser"))
QStringList aRecentlyOpenedFiles;
TInspectorEXE_OpenFileDialog::GetPluginRecentlyOpenedFiles (theName, theCommunicator, aRecentlyOpenedFiles);
TCollection_AsciiString aFileName, anAdditionalFileName;
if (!aRecentlyOpenedFiles.isEmpty())
aFileName = TCollection_AsciiString (aRecentlyOpenedFiles.last().toUtf8().data());
if (aFileName.IsEmpty())
{
theCommunicator->OpenFile (theName, fileNameInDataDir ("CSF_OCCTDataPath", "step/screw.step"));
theButtonControl->SetPluginDir (theName, fileNameInDataDir ("CSF_OCCTDataPath", "step"));
}
else if (theName.IsEqual ("TKShapeView"))
{
theCommunicator->OpenFile (theName, fileNameInDataDir ("CSF_OCCTDataPath", "occ/hammer.brep"));
theButtonControl->SetPluginDir (theName, fileNameInDataDir ("CSF_OCCTDataPath", "occ"));
}
else if (theName.IsEqual ("TKVInspector"))
{
theCommunicator->OpenFile (theName, fileNameInDataDir ("CSF_OCCTDataPath", "occ/face1.brep"));
theCommunicator->OpenFile (theName, fileNameInDataDir ("CSF_OCCTDataPath", "occ/face2.brep"));
theButtonControl->SetPluginDir (theName, fileNameInDataDir ("CSF_OCCTDataPath", "occ"));
if (theName.IsEqual ("TKDFBrowser"))
aFileName = fileNameInDataDir ("CSF_OCCTDataPath", "step/screw.step");
else if (theName.IsEqual ("TKShapeView"))
aFileName = fileNameInDataDir ("CSF_OCCTDataPath", "occ/hammer.brep");
else if (theName.IsEqual ("TKVInspector"))
{
aFileName = fileNameInDataDir ("CSF_OCCTDataPath", "occ/face1.brep");
anAdditionalFileName = fileNameInDataDir ("CSF_OCCTDataPath", "occ/face2.brep");
}
aRecentlyOpenedFiles.append (aFileName.ToCString());
if (!anAdditionalFileName.IsEmpty())
aRecentlyOpenedFiles.append (anAdditionalFileName.ToCString());
}
theCommunicator->OpenFile (theName, aFileName.ToCString());
if (!anAdditionalFileName.IsEmpty())
theCommunicator->OpenFile (theName, anAdditionalFileName);

theButtonControl->SetPluginRecentlyOpenedFiles (theName, aRecentlyOpenedFiles);
}

// =======================================================================
Expand Down
139 changes: 82 additions & 57 deletions tools/TInspectorEXE/TInspectorEXE_OpenFileDialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const int OPEN_DIALOG_HEIGHT = 200;
const int MARGIN_DIALOG = 4;
const int SPACING_DIALOG = 2;

const int RECENT_FILES_CACHE_SIZE = 10;

TInspector_Communicator* MyCommunicator;

// =======================================================================
Expand Down Expand Up @@ -75,15 +77,36 @@ void TInspectorEXE_OpenButton::onStartButtonClicked()
if (aPluginName.IsEmpty())
return;

QString aDataDirName = QDir::currentPath();
if (myDefaultDirs.IsBound (aPluginName))
aDataDirName = myDefaultDirs.Find (aPluginName).ToCString();
QStringList aPluginRecentlyOpenedFiles;
if (myRecentlyOpenedFiles.contains(aPluginName))
{
QStringList aFileNames = myRecentlyOpenedFiles[aPluginName];
for (int i = 0; i < aFileNames.size(); i++)
{
QFileInfo aFileInfo (aFileNames[i]);
if (aFileInfo.exists() && aFileInfo.isFile())
aPluginRecentlyOpenedFiles.append(aFileInfo.absoluteFilePath());
}
}

QString aFileName = TInspectorEXE_OpenFileDialog::OpenFile (0, aDataDirName);
QString aFileName = TInspectorEXE_OpenFileDialog::OpenFile (0, aPluginRecentlyOpenedFiles);
aFileName = QDir().toNativeSeparators (aFileName);
if (!aFileName.isEmpty()) {
QApplication::setOverrideCursor (Qt::WaitCursor);
TInspectorEXE_OpenFileDialog::Communicator()->OpenFile (aPluginName, TCollection_AsciiString (aFileName.toUtf8().data()));

QFileInfo aFileInfo (aFileName);
if (!aPluginRecentlyOpenedFiles.contains (aFileInfo.absoluteFilePath()))
{
myRecentlyOpenedFiles[aPluginName].append (aFileInfo.absoluteFilePath());
for (int i = 0; i < myRecentlyOpenedFiles[aPluginName].size() - RECENT_FILES_CACHE_SIZE; i++)
myRecentlyOpenedFiles[aPluginName].removeFirst();
TInspectorEXE_OpenFileDialog::SetPluginRecentlyOpenedFiles (aPluginName,
TInspectorEXE_OpenFileDialog::Communicator(), myRecentlyOpenedFiles[aPluginName]);

TInspectorEXE_OpenFileDialog::Communicator()->GetPluginParameters()->StorePreferences();
}

QApplication::restoreOverrideCursor();
}
}
Expand All @@ -102,26 +125,21 @@ void changeMargins (QBoxLayout* theLayout)
// function : Constructor
// purpose :
// =======================================================================
TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent, const QString& theDataDirName)
: QDialog(theParent), myDataDir (theDataDirName)
TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent, const QStringList& theRecentlyOpenedFiles)
: QDialog (theParent), myRecentlyOpenedFiles (theRecentlyOpenedFiles)
{
setWindowTitle (theDataDirName);
setWindowTitle (tr ("Open File"));

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"));
aSamplesBox->setTitle (tr ("Recent files"));
aDialogLay->addWidget (aSamplesBox);
QVBoxLayout* aSampleLay = new QVBoxLayout (aSamplesBox);
changeMargins (aSampleLay);
mySamplesView = createTableView (readSampleNames());
mySamplesView = createTableView (theRecentlyOpenedFiles);
aSampleLay->addWidget (mySamplesView);

// Select file
Expand All @@ -143,16 +161,8 @@ TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent,
aSelectFileBtn->setIcon (QIcon (":folder_open.png"));
aSelectFileLay->addWidget (aSelectFileBtn, 1, 1);

myFolderApplyOpen = new QToolButton (aSelectFileBox);
myFolderApplyOpen->setIcon (QIcon (":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()));
connect (mySelectedName, SIGNAL (returnPressed()), this, SLOT (onApplySelectClicked()));

resize (OPEN_DIALOG_WIDTH, OPEN_DIALOG_HEIGHT);
}
Expand All @@ -161,10 +171,10 @@ TInspectorEXE_OpenFileDialog::TInspectorEXE_OpenFileDialog (QWidget* theParent,
// function : OpenFile
// purpose :
// =======================================================================
QString TInspectorEXE_OpenFileDialog::OpenFile (QWidget* theParent, const QString& theDataDirName)
QString TInspectorEXE_OpenFileDialog::OpenFile (QWidget* theParent, const QStringList& theRecentlyOpenedFiles)
{
QString aFileName;
TInspectorEXE_OpenFileDialog* aDialog = new TInspectorEXE_OpenFileDialog(theParent, theDataDirName);
TInspectorEXE_OpenFileDialog* aDialog = new TInspectorEXE_OpenFileDialog (theParent, theRecentlyOpenedFiles);
if (aDialog->exec() == QDialog::Accepted)
aFileName = aDialog->GetFileName();

Expand All @@ -182,6 +192,47 @@ TInspector_Communicator* TInspectorEXE_OpenFileDialog::Communicator()
return MyCommunicator;
}

// =======================================================================
// function : GetPluginRecentlyOpenedFiles
// purpose :
// =======================================================================
void TInspectorEXE_OpenFileDialog::GetPluginRecentlyOpenedFiles (const TCollection_AsciiString& thePluginName,
TInspector_Communicator* theCommunicator,
QStringList& theFileNames)
{
Handle(TInspectorAPI_PluginParameters) aParameters = theCommunicator->GetPluginParameters();
TInspectorAPI_PreferencesDataMap aPreferencesItem;
aParameters->GetPreferences (thePluginName, aPreferencesItem);

for (TInspectorAPI_IteratorOfPreferencesDataMap anItemIt (aPreferencesItem); anItemIt.More(); anItemIt.Next())
{
if (!anItemIt.Key().IsEqual("recently_opened_files"))
continue;
theFileNames = QString(anItemIt.Value().ToCString()).split(";", QString::SkipEmptyParts);
if (theFileNames.size() > RECENT_FILES_CACHE_SIZE)
for (int i = 0; i < theFileNames.size() - RECENT_FILES_CACHE_SIZE; i++)
theFileNames.removeFirst();
break;
}
}

// =======================================================================
// function : SetPluginRecentlyOpenedFiles
// purpose :
// =======================================================================
void TInspectorEXE_OpenFileDialog::SetPluginRecentlyOpenedFiles (const TCollection_AsciiString& thePluginName,
TInspector_Communicator* theCommunicator,
QStringList& theFileNames)
{
Handle(TInspectorAPI_PluginParameters) aParameters = theCommunicator->GetPluginParameters();

TInspectorAPI_PreferencesDataMap aPreferencesItem;
aParameters->GetPreferences (thePluginName, aPreferencesItem);
aPreferencesItem.Bind ("recently_opened_files", TCollection_AsciiString (theFileNames.join (";").toUtf8().data()));

aParameters->SetPreferences (thePluginName, aPreferencesItem);
}

// =======================================================================
// function : onSampleSelectionChanged
// purpose :
Expand All @@ -203,17 +254,6 @@ void TInspectorEXE_OpenFileDialog::onSampleSelectionChanged (const QItemSelectio
accept();
}

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

// =======================================================================
// function : onSelectClicked
// purpose :
Expand Down Expand Up @@ -245,6 +285,12 @@ void TInspectorEXE_OpenFileDialog::onSelectClicked()
// =======================================================================
void TInspectorEXE_OpenFileDialog::onApplySelectClicked()
{
QString aFileName = mySelectedName->text();

QFileInfo aFileInfo (aFileName);
if (!aFileInfo.exists() || !aFileInfo.isFile())
return;

myFileName = mySelectedName->text();
accept();
}
Expand Down Expand Up @@ -291,24 +337,3 @@ QAbstractItemModel* TInspectorEXE_OpenFileDialog::createModel (const QStringList
aModel->Init (theFileNames);
return aModel;
}

// =======================================================================
// function : readSampleNames
// purpose :
// =======================================================================
QStringList TInspectorEXE_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;
}
Loading

0 comments on commit 042e09a

Please sign in to comment.