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/TInspector_Window.hxx>
17 #include <inspector/TInspector_Window.hxx>
19 #include <inspector/TInspectorAPI_Communicator.hxx>
20 #include <inspector/TInspector_PluginParameters.hxx>
22 #include <QApplication>
23 #include <QDockWidget>
25 #include <QMainWindow>
26 #include <QPushButton>
27 #include <QStackedWidget>
28 #include <QVBoxLayout>
30 const int TINSPECTOR_DEFAULT_WIDTH = 900;
31 const int TINSPECTOR_DEFAULT_HEIGHT = 700;
32 const int TINSPECTOR_DEFAULT_POSITION_X = 200;
33 const int TINSPECTOR_DEFAULT_POSITION_Y = 60;
35 // =======================================================================
36 // function : Constructor
38 // =======================================================================
39 TInspector_Window::TInspector_Window()
40 : QObject(), myOpenButton (0)
42 myMainWindow = new QMainWindow();
44 QWidget* aCentralWidget = new QWidget (myMainWindow);
45 myMainWindow->setCentralWidget (aCentralWidget);
46 QVBoxLayout* aCentralLayout = new QVBoxLayout (aCentralWidget);
47 aCentralLayout->setContentsMargins (0, 0, 0, 0);
48 aCentralLayout->setSpacing (0);
50 myToolsStack = new QStackedWidget (aCentralWidget);
51 myToolsStack->setFrameShape (QFrame::Box);
52 aCentralLayout->addWidget (myToolsStack);
54 myEmptyWidget = new QWidget (aCentralWidget);
55 myToolsStack->addWidget (myEmptyWidget);
57 myButtonWidget = new QWidget (aCentralWidget);
58 myButtonLay = new QHBoxLayout (myButtonWidget);
59 myButtonLay->setContentsMargins (0, 0, 0, 0);
60 myButtonLay->setSpacing (0);
61 myButtonLay->insertStretch (0, 1);
63 aCentralLayout->addWidget (myButtonWidget);
64 aCentralLayout->addWidget (myToolsStack);
66 myMainWindow->resize (TINSPECTOR_DEFAULT_WIDTH, TINSPECTOR_DEFAULT_HEIGHT);
67 myMainWindow->move (TINSPECTOR_DEFAULT_POSITION_X, TINSPECTOR_DEFAULT_POSITION_Y);
68 myMainWindow->setDockOptions (QMainWindow::VerticalTabs);
70 myParameters = new TInspector_PluginParameters (this);
73 // =======================================================================
74 // function : RegisterPlugin
76 // =======================================================================
77 void TInspector_Window::RegisterPlugin (const TCollection_AsciiString& thePluginName)
79 TInspector_ToolInfo anInfo;
81 if (FindPlugin (thePluginName, anInfo, aToolId))
84 myToolNames.append (TInspector_ToolInfo (thePluginName));
87 // =======================================================================
88 // function : RegisteredPlugins
90 // =======================================================================
91 NCollection_List<TCollection_AsciiString> TInspector_Window::RegisteredPlugins() const
93 NCollection_List<TCollection_AsciiString> aPlugins;
95 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
96 aPlugins.Append (myToolNames[aToolId].myName);
101 // =======================================================================
104 // =======================================================================
105 void TInspector_Window::Init (const TCollection_AsciiString& thePluginName,
106 const NCollection_List<Handle(Standard_Transient)>& theParameters,
107 const Standard_Boolean theAppend)
109 if (thePluginName.IsEmpty())
111 // Init all plugins by the given parameters
112 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
113 Init (myToolNames[aToolId].myName, theParameters, theAppend);
115 // temporary activation of the first tool
116 if (!myToolNames.isEmpty())
117 ActivateTool (myToolNames[0].myName);
123 NCollection_List<Handle(Standard_Transient)> aParameters;
124 if (myParameters->FindParameters (thePluginName))
125 aParameters = myParameters->Parameters (thePluginName);
127 for (NCollection_List<Handle(Standard_Transient)>::Iterator anIterator (theParameters);
128 anIterator.More(); anIterator.Next())
129 aParameters.Append (anIterator.Value());
131 myParameters->SetParameters (thePluginName, aParameters, Standard_False);
134 myParameters->SetParameters (thePluginName, theParameters, Standard_False);
136 TInspector_ToolInfo anInfo;
138 if (!FindPlugin (thePluginName, anInfo, aToolId))
144 QString aButtonName = anInfo.myName.ToCString();
145 if (aButtonName.indexOf("TK") == 0)
146 aButtonName = aButtonName.mid(2);
148 QPushButton* aButton = new QPushButton(aButtonName, myButtonWidget);
149 connect (aButton, SIGNAL (clicked (bool)), this, SLOT (onButtonClicked()));
150 myButtonLay->insertWidget (myButtonLay->count()-1, aButton);
151 anInfo.myButton = aButton;
152 myToolNames[aToolId] = anInfo;
155 // =======================================================================
156 // function : ActivateTool
158 // =======================================================================
159 void TInspector_Window::ActivateTool (const TCollection_AsciiString& thePluginName)
162 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
164 if (myToolNames[aToolId].myName != thePluginName)
166 aToolIndex = aToolId;
173 TInspector_ToolInfo anInfo = myToolNames[aToolIndex];
174 if (!anInfo.myWidget)
176 if (!LoadPlugin (thePluginName, anInfo))
178 anInfo.myButton->setEnabled (false);
181 myToolsStack->addWidget (anInfo.myWidget);
182 myToolNames[aToolIndex] = anInfo;
185 QWidget* aWidget = anInfo.myWidget;
186 myToolsStack->setCurrentWidget (aWidget);
188 myOpenButton->setObjectName (thePluginName.ToCString());
190 anInfo.myCommunicator->UpdateContent();
191 onCommuncatorNameChanged();
194 // =======================================================================
195 // function : SetSelected
197 // =======================================================================
198 void TInspector_Window::SetSelected (const NCollection_List<TCollection_AsciiString>& theItemNames)
200 TInspector_ToolInfo anInfo;
201 if (!ActiveToolInfo (anInfo))
204 myParameters->SetSelectedNames (anInfo.myName, theItemNames);
206 TInspectorAPI_Communicator* aCommunicator = anInfo.myCommunicator;
209 aCommunicator->UpdateContent();
213 // =======================================================================
214 // function : SetSelected
216 // =======================================================================
217 void TInspector_Window::SetSelected (const NCollection_List<Handle(Standard_Transient)>& theObjects)
219 TInspector_ToolInfo anInfo;
220 if (!ActiveToolInfo (anInfo))
223 myParameters->SetSelected (anInfo.myName, theObjects);
225 TInspectorAPI_Communicator* aCommunicator = anInfo.myCommunicator;
228 aCommunicator->UpdateContent();
232 // =======================================================================
233 // function : SetOpenButton
235 // =======================================================================
236 void TInspector_Window::SetOpenButton (QPushButton* theButton)
238 myOpenButton = theButton;
239 TInspector_ToolInfo anInfo;
240 if (ActiveToolInfo (anInfo))
241 myOpenButton->setObjectName (anInfo.myName.ToCString());
242 myButtonLay->insertWidget (0, theButton);
245 // =======================================================================
246 // function : OpenFile
248 // =======================================================================
249 void TInspector_Window::OpenFile (const TCollection_AsciiString& thePluginName,
250 const TCollection_AsciiString& theFileName)
252 if (thePluginName.IsEmpty())
254 // Apply file name to all plugins
255 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
256 OpenFile (myToolNames[aToolId].myName, theFileName);
260 myParameters->AddFileName (thePluginName, theFileName);
262 TInspector_ToolInfo anInfo;
263 if (!ActiveToolInfo (anInfo) || anInfo.myName != thePluginName)
266 TInspectorAPI_Communicator* aCommunicator = anInfo.myCommunicator;
268 aCommunicator->UpdateContent();
271 // =======================================================================
272 // function : UpdateContent
274 // =======================================================================
275 void TInspector_Window::UpdateContent()
277 TInspector_ToolInfo anInfo;
278 if (!ActiveToolInfo (anInfo) || !anInfo.myCommunicator)
281 anInfo.myCommunicator->UpdateContent();
284 // =======================================================================
285 // function : LoadPlugin
287 // =======================================================================
288 bool TInspector_Window::LoadPlugin (const TCollection_AsciiString& thePluginName, TInspector_ToolInfo& theInfo)
290 bool aLoaded = false;
292 QApplication::setOverrideCursor (Qt::WaitCursor);
293 TInspectorAPI_Communicator* aCommunicator = TInspectorAPI_Communicator::LoadPluginLibrary (thePluginName);
297 aCommunicator->SetParameters (myParameters);
298 QWidget* aParentWidget = new QWidget (myMainWindow);
299 QVBoxLayout* aLayout = new QVBoxLayout (aParentWidget);
300 aLayout->setContentsMargins (0, 0, 0, 0);
301 aLayout->setSpacing (0);
302 aParentWidget->setLayout (aLayout);
303 aCommunicator->SetParent (aParentWidget);
304 theInfo.myWidget = aParentWidget;
305 theInfo.myCommunicator = aCommunicator;
306 #if QT_VERSION >= 0x050000
307 connect (aParentWidget, SIGNAL (objectNameChanged (const QString&)), this, SLOT (onCommuncatorNameChanged()));
311 QApplication::restoreOverrideCursor();
315 // =======================================================================
316 // function : onButtonClicked
318 // =======================================================================
319 void TInspector_Window::onButtonClicked()
321 QPushButton* aButton = (QPushButton*)sender();
322 ActivateTool (TCollection_AsciiString ("TK") + aButton->text().toStdString().c_str());
325 // =======================================================================
326 // function : onCommuncatorNameChanged
328 // =======================================================================
329 void TInspector_Window::onCommuncatorNameChanged()
331 #if QT_VERSION >= 0x050000
332 TInspector_ToolInfo anInfo;
333 if (!ActiveToolInfo (anInfo))
335 myMainWindow->setWindowTitle (anInfo.myWidget->objectName());
339 // =======================================================================
340 // function : ActiveToolInfo
342 // =======================================================================
343 bool TInspector_Window::ActiveToolInfo (TInspector_Window::TInspector_ToolInfo& theToolInfo) const
345 QWidget* anActiveWidget = myToolsStack->currentWidget();
346 if (anActiveWidget == myEmptyWidget)
349 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
351 if (myToolNames[aToolId].myWidget && myToolNames[aToolId].myWidget == anActiveWidget)
353 theToolInfo = myToolNames[aToolId];
360 // =======================================================================
361 // function : FindPlugin
363 // =======================================================================
364 bool TInspector_Window::FindPlugin (const TCollection_AsciiString& thePluginName, TInspector_ToolInfo& theToolInfo,
367 for (int aToolId = 0, aSize = myToolNames.size(); aToolId < aSize; aToolId++)
369 TInspector_ToolInfo anInfo = myToolNames[aToolId];
370 if (anInfo.myName != thePluginName)
372 theToolInfo = anInfo;