preferencesdialog.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. = StarPU-Top for StarPU =
  3. Copyright (C) 2011
  4. William Braik
  5. Yann Courtois
  6. Jean-Marie Couteyen
  7. Anthony Roy
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "preferencesdialog.h"
  21. #include "ui_preferencesdialog.h"
  22. #include "mainwindow.h"
  23. #include "configurationmanager.h"
  24. #include "sessionsetupmanager.h"
  25. #include <QCloseEvent>
  26. #include <QMessageBox>
  27. #include <QDir>
  28. const QString DEFAULT_SESSION_SETUP_NAME = "<default>";
  29. PreferencesDialog::PreferencesDialog(MainWindow *mainWindow,
  30. ConfigurationManager *configurationManager,
  31. SessionSetupManager *sessionSetupManger,
  32. int activeTab) :
  33. QDialog(mainWindow), ui(new Ui::PreferencesDialog)
  34. {
  35. ui->setupUi(this);
  36. _mainWindow = mainWindow;
  37. _configurationManager = configurationManager;
  38. _sessionSetupManager = sessionSetupManger;
  39. _dataWidgetNames = _mainWindow->dataWidgetNames();
  40. _dataWidgetPossibilities = _mainWindow->dataWidgetPossibilities();
  41. _interactiveWidgetNames = _mainWindow->interactiveWidgetNames();
  42. _interactiveWidgetPossibilities
  43. = _mainWindow->interactiveWidgetPossibilities();
  44. ui->ipEdit->setText(_configurationManager->serverHost());
  45. ui->portEdit->setValue(_configurationManager->serverPort());
  46. ui->commandEdit->setText(_configurationManager->commandLine());
  47. ui->connectionSSHGroupBox->setChecked(_configurationManager);
  48. ui->antialiasingCheckBox->setChecked(_configurationManager->antialiasing());
  49. ui->displayModeComboBox->addItem(tr("Flying windows"),
  50. DISPLAY_FLYING_WINDOWS);
  51. ui->displayModeComboBox->addItem(tr("MDI, normal"), DISPLAY_MDI_NORMAL);
  52. ui->preferencesTab->setCurrentIndex(activeTab);
  53. // Synchronize display mode indexes with the main window's toolbar
  54. QObject::connect(_mainWindow, SIGNAL(preferencesDialogCreated(int)),
  55. this, SLOT(updateDisplayModeIndex(int)));
  56. QObject::connect(ui->displayModeComboBox, SIGNAL(currentIndexChanged(int)),
  57. _mainWindow, SLOT(updateDisplayModeIndex(int)));
  58. // Synchronize descriptions setup when session setup is loaded
  59. QObject::connect(
  60. _mainWindow->sessionSetupManager(),
  61. SIGNAL(
  62. descriptionsSetupLoaded(QList<DataDescriptionSetup> ,
  63. QList<ParamDescriptionSetup> )),
  64. this,
  65. SLOT(
  66. sessionDescriptionsSetupLoaded(
  67. QList<DataDescriptionSetup> ,
  68. QList<ParamDescriptionSetup> )));
  69. // Synchronize main window setup when session setup is loaded
  70. QObject::connect(_mainWindow->sessionSetupManager(),
  71. SIGNAL(mainWindowSetupLoaded(MainWindowSetup)), this,
  72. SLOT(sessionMainWindowSetupLoaded(MainWindowSetup)));
  73. if (_mainWindow->isConnected())
  74. {
  75. ui->connectionServerGroupBox->setEnabled(false);
  76. }
  77. if (_mainWindow->isReady() == false)
  78. {
  79. ui->displayWidgetsGroupBox->setEnabled(false);
  80. }
  81. displayWidgetPreferences();
  82. displaySessionSetupsList();
  83. }
  84. PreferencesDialog::~PreferencesDialog()
  85. {
  86. delete ui;
  87. }
  88. void PreferencesDialog::changeEvent(QEvent *e)
  89. {
  90. QDialog::changeEvent(e);
  91. switch (e->type())
  92. {
  93. case QEvent::LanguageChange:
  94. ui->retranslateUi(this);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. void PreferencesDialog::closeEvent(QCloseEvent *ce)
  101. {
  102. _configurationManager->setServerHost(ui->ipEdit->text());
  103. _configurationManager->setServerPort(ui->portEdit->value());
  104. _configurationManager->setCommandLine(ui->commandEdit->text());
  105. _configurationManager->setAntialiasing(
  106. ui->antialiasingCheckBox->isChecked());
  107. _configurationManager->setSSH(ui->connectionSSHGroupBox->isChecked());
  108. ce->accept();
  109. }
  110. void PreferencesDialog::displayWidgetPreferences()
  111. {
  112. // Read data descriptions
  113. for (int i = 0; i < _mainWindow->dataDescriptions()->count(); i++)
  114. {
  115. DataDescription *dataDescription = _mainWindow->dataDescriptions()->at(
  116. i);
  117. // Init UI
  118. QGridLayout *layout = (QGridLayout*) ui->dataWidgetsGroupBox->layout();
  119. layout->addWidget(new QLabel(dataDescription->descriptionString), i, 0,
  120. Qt::AlignHCenter);
  121. QComboBox *dataWidgetList = new QComboBox();
  122. dataWidgetList->setMinimumWidth(100);
  123. // Init list
  124. int dataId = dataDescription->id;
  125. DataType dataType = dataDescription->type;
  126. DataWidgetListItemData dataStruct;
  127. dataStruct.id = dataId;
  128. QVariant data;
  129. QList < DataWidgetType > dataWidgetPossibilities
  130. = _dataWidgetPossibilities->value(dataType).values();
  131. int currentDataWidgetIndex;
  132. for (int j = 0; j < dataWidgetPossibilities.count(); j++)
  133. {
  134. DataWidgetType widget = dataWidgetPossibilities.at(j);
  135. if (widget == dataDescription->widget)
  136. {
  137. currentDataWidgetIndex = j;
  138. }
  139. dataStruct.widget = widget;
  140. data.setValue(dataStruct);
  141. dataWidgetList->addItem(_dataWidgetNames->value(widget), data);
  142. }
  143. // Last item : MUST be DATA_WIDGET_NONE
  144. dataStruct.widget = DATA_WIDGET_NONE;
  145. data.setValue(dataStruct);
  146. dataWidgetList->addItem(
  147. _dataWidgetNames->value(DATA_WIDGET_NONE), data);
  148. // Init index
  149. if (dataDescription->widget == DATA_WIDGET_NONE)
  150. { // NONE is the last item
  151. dataWidgetList->setCurrentIndex(dataWidgetList->count() - 1);
  152. }
  153. else
  154. {
  155. dataWidgetList->setCurrentIndex(currentDataWidgetIndex);
  156. }
  157. QObject::connect(dataWidgetList, SIGNAL(currentIndexChanged(int)),
  158. this, SLOT(updateDataWidgetType(int)));
  159. _dataWidgetListComboBoxes.append(dataWidgetList);
  160. layout->addWidget(dataWidgetList, i, 1, Qt::AlignHCenter);
  161. }
  162. // Param descriptions
  163. for (int i = 0; i < _mainWindow->paramDescriptions()->count(); i++)
  164. {
  165. ParamDescription *paramDescription =
  166. _mainWindow->paramDescriptions()->at(i);
  167. QGridLayout *layout =
  168. (QGridLayout*) ui->interactiveWidgetsGroupBox->layout();
  169. layout->addWidget(new QLabel(paramDescription->descriptionString), i,
  170. 0, Qt::AlignHCenter);
  171. QComboBox *interactiveWidgetList = new QComboBox();
  172. interactiveWidgetList->setMinimumWidth(100);
  173. // Init list
  174. int paramId = paramDescription->id;
  175. ParamType paramType = paramDescription->type;
  176. InteractiveWidgetListItemData dataStruct;
  177. dataStruct.id = paramId;
  178. QVariant data;
  179. QList < InteractiveWidgetType > interactiveWidgetPossibilities
  180. = _interactiveWidgetPossibilities->value(paramType).values();
  181. int currentInteractiveWidgetIndex;
  182. for (int j = 0; j < interactiveWidgetPossibilities.count(); j++)
  183. {
  184. InteractiveWidgetType widget = interactiveWidgetPossibilities.at(j);
  185. if (widget == paramDescription->widget)
  186. {
  187. currentInteractiveWidgetIndex = j;
  188. }
  189. dataStruct.widget = widget;
  190. data.setValue(dataStruct);
  191. interactiveWidgetList->addItem(
  192. _interactiveWidgetNames->value(widget), data);
  193. }
  194. // Init index
  195. interactiveWidgetList->setCurrentIndex(currentInteractiveWidgetIndex);
  196. QObject::connect(interactiveWidgetList,
  197. SIGNAL(currentIndexChanged(int)),
  198. this, SLOT(updateInteractiveWidgetType(int)));
  199. _interactiveWidgetListComboBoxes.append(interactiveWidgetList);
  200. layout->addWidget(interactiveWidgetList, i, 1, Qt::AlignHCenter);
  201. }
  202. }
  203. void PreferencesDialog::displaySessionSetupsList()
  204. {
  205. // Default session setup must be the first index
  206. ui->loadSessionSetupComboBox->addItem(DEFAULT_SESSION_SETUP_NAME);
  207. QDir sessionSetupsDir = QDir(SESSION_SETUPS_DIR);
  208. QStringList filter;
  209. filter << "*" + SESSION_SETUPS_FILEEXT;
  210. sessionSetupsDir.setNameFilters(filter);
  211. sessionSetupsDir.setSorting(QDir::Time);
  212. QStringList sessionSetupFiles = sessionSetupsDir.entryList();
  213. QStringList sessionSetupNames;
  214. for (int i = 0; i < sessionSetupFiles.count(); i++)
  215. {
  216. QString sessionSetupFile = sessionSetupFiles.at(i);
  217. QString sessionSetupName = sessionSetupFile;
  218. sessionSetupName.chop(SESSION_SETUPS_FILEEXT.size());
  219. sessionSetupNames.append(sessionSetupName);
  220. }
  221. ui->loadSessionSetupComboBox->addItems(sessionSetupNames);
  222. ui->loadSessionSetupComboBox->setCurrentIndex(0);
  223. QObject::connect(ui->loadSessionSetupComboBox,
  224. SIGNAL(currentIndexChanged(QString)), this,
  225. SLOT(loadSessionSetup(QString)));
  226. }
  227. void PreferencesDialog::updateDisplayModeIndex(int index)
  228. {
  229. ui->displayModeComboBox->setCurrentIndex(index);
  230. }
  231. void PreferencesDialog::updateDataWidgetType(int index)
  232. {
  233. QComboBox *dataWidgetList = (QComboBox*) QObject::sender();
  234. QVariant data = dataWidgetList->itemData(index, Qt::UserRole);
  235. DataWidgetListItemData dataStruct;
  236. Q_ASSERT_X(data.canConvert<DataWidgetListItemData> () == true,
  237. "PreferencesDialog::updateDataWidgetType()",
  238. "Data extraction from QVariant failed !");
  239. dataStruct = data.value<DataWidgetListItemData> ();
  240. int dataId = dataStruct.id;
  241. DataWidgetType newWidget = dataStruct.widget;
  242. _mainWindow->updateDataWidgetType(dataId, newWidget);
  243. }
  244. void PreferencesDialog::updateInteractiveWidgetType(int index)
  245. {
  246. QComboBox *interactiveWidgetList = (QComboBox*) QObject::sender();
  247. QVariant data = interactiveWidgetList->itemData(index, Qt::UserRole);
  248. InteractiveWidgetListItemData dataStruct;
  249. Q_ASSERT_X(data.canConvert<InteractiveWidgetListItemData> () == true,
  250. "PreferencesDialog::updateInteractiveWidgetType()",
  251. "Data extraction from QVariant failed !");
  252. dataStruct = data.value<InteractiveWidgetListItemData> ();
  253. int paramId = dataStruct.id;
  254. InteractiveWidgetType newWidget = dataStruct.widget;
  255. _mainWindow->updateInteractiveWidgetType(paramId, newWidget);
  256. }
  257. void PreferencesDialog::loadSessionSetup(QString sessionSetupName)
  258. {
  259. if (sessionSetupName.compare(DEFAULT_SESSION_SETUP_NAME) == 0)
  260. {
  261. qDebug() << "PreferencesDialog : loading default session setup";
  262. // Load default data widgets
  263. loadDefaultDataWidgets();
  264. // Load default interactive widgets
  265. loadDefaultInteractiveWidgets();
  266. }
  267. else
  268. {
  269. qDebug() << "PreferencesDialog : loading session setup"
  270. << sessionSetupName;
  271. if (_sessionSetupManager->loadSessionSetup(sessionSetupName) == true)
  272. {
  273. qDebug() << "PreferencesDialog : loaded session setup successfully";
  274. }
  275. else
  276. {
  277. QMessageBox::warning(this, tr("StarPU-Top"),
  278. tr("Couldn't load the session setup !"),
  279. QMessageBox::Ok);
  280. QComboBox *comboBox = (QComboBox*) QObject::sender();
  281. int indexToRemove = comboBox->currentIndex();
  282. comboBox->setCurrentIndex(0);
  283. comboBox->removeItem(indexToRemove);
  284. }
  285. }
  286. }
  287. void PreferencesDialog::loadDefaultDataWidgets()
  288. {
  289. for (int i = 0; i < _dataWidgetListComboBoxes.count(); i++)
  290. {
  291. for (int j = 0; _dataWidgetListComboBoxes.at(i)->count(); j++)
  292. {
  293. Q_ASSERT_X(
  294. _dataWidgetListComboBoxes.at(i)->itemData(j). canConvert<
  295. DataWidgetListItemData> () == true,
  296. "PreferencesDialog::loadSessionSetup()",
  297. "Data extraction from QVariant failed !");
  298. DataWidgetListItemData dataStruct =
  299. _dataWidgetListComboBoxes.at(i)->itemData(j).value<
  300. DataWidgetListItemData> ();
  301. if (dataStruct.widget == DEFAULT_DATA_WIDGET_BOOL
  302. || dataStruct.widget == DEFAULT_DATA_WIDGET_INT
  303. || dataStruct.widget == DEFAULT_DATA_WIDGET_FLOAT)
  304. {
  305. _dataWidgetListComboBoxes.at(i)->setCurrentIndex(j);
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. void PreferencesDialog::loadDefaultInteractiveWidgets()
  312. {
  313. for (int i = 0; i < _interactiveWidgetListComboBoxes.count(); i++)
  314. {
  315. for (int j = 0; _interactiveWidgetListComboBoxes.at(i)->count(); j++)
  316. {
  317. Q_ASSERT_X(
  318. _interactiveWidgetListComboBoxes.
  319. at(i)->itemData(j). canConvert<
  320. InteractiveWidgetListItemData> () == true,
  321. "PreferencesDialog::loadSessionSetup()",
  322. "Data extraction from QVariant failed !");
  323. InteractiveWidgetListItemData dataStruct =
  324. _interactiveWidgetListComboBoxes.at(i) ->itemData(j).value<
  325. InteractiveWidgetListItemData> ();
  326. if (dataStruct.widget == DEFAULT_INTERACTIVE_WIDGET_BOOL
  327. || dataStruct.widget == DEFAULT_INTERACTIVE_WIDGET_INT
  328. || dataStruct.widget == DEFAULT_INTERACTIVE_WIDGET_FLOAT
  329. || dataStruct.widget == DEFAULT_INTERACTIVE_WIDGET_ENUM)
  330. {
  331. _interactiveWidgetListComboBoxes.at(i)->setCurrentIndex(j);
  332. break;
  333. }
  334. }
  335. }
  336. }
  337. void PreferencesDialog::sessionDescriptionsSetupLoaded(
  338. QList<DataDescriptionSetup> dataDescriptionsSetup,
  339. QList<ParamDescriptionSetup> paramDescriptionsSetup)
  340. {
  341. qDebug()
  342. << "PreferencesDialog : "
  343. "restoring descriptions setup from another session";
  344. // Load data descriptions setup
  345. for (int i = 0; i < dataDescriptionsSetup.count(); i++)
  346. {
  347. // The comboboxes are created following the order of the descriptions
  348. int newIndex = indexOfDataWidget(_dataWidgetListComboBoxes.at(i),
  349. dataDescriptionsSetup.at(i).widget);
  350. Q_ASSERT_X(newIndex != -1, "PreferencesDialog::sessionSetupLoaded()",
  351. "Couldn't find loaded data widget index in the list !");
  352. _dataWidgetListComboBoxes.at(i)->setCurrentIndex(newIndex);
  353. }
  354. // Load param descriptions setup
  355. for (int i = 0; i < paramDescriptionsSetup.count(); i++)
  356. {
  357. // The comboboxes are created following the order of the descriptions
  358. int newIndex = indexOfInteractiveWidget(
  359. _interactiveWidgetListComboBoxes.at(i),
  360. paramDescriptionsSetup.at(i).widget);
  361. Q_ASSERT_X(newIndex != -1, "PreferencesDialog::sessionSetupLoaded()",
  362. "Couldn't find loaded interactive widget index in the list !");
  363. _interactiveWidgetListComboBoxes.at(i)->setCurrentIndex(newIndex);
  364. }
  365. }
  366. void PreferencesDialog::sessionMainWindowSetupLoaded(
  367. MainWindowSetup mainWindowSetup)
  368. {
  369. if (_mainWindow->displayMode() != mainWindowSetup.displayMode)
  370. {
  371. for (int i = 0; i < ui->displayModeComboBox->count(); i++)
  372. {
  373. DisplayMode displayMode =
  374. (DisplayMode) ui->displayModeComboBox ->itemData(i).toInt();
  375. if (displayMode == mainWindowSetup.displayMode)
  376. {
  377. ui->displayModeComboBox->setCurrentIndex(i);
  378. return;
  379. }
  380. }
  381. }
  382. }
  383. // Util methods
  384. int PreferencesDialog::indexOfDataWidget(QComboBox* dataWidgetList,
  385. DataWidgetType widget)
  386. {
  387. for (int i = 0; dataWidgetList->count(); i++)
  388. {
  389. Q_ASSERT_X(
  390. dataWidgetList->itemData(i)
  391. . canConvert<DataWidgetListItemData> ()
  392. == true, "PreferencesDialog::indexOfDataWidget()",
  393. "Data extraction from QVariant failed !");
  394. DataWidgetListItemData dataStruct = dataWidgetList->itemData(i).value<
  395. DataWidgetListItemData> ();
  396. if (dataStruct.widget == widget)
  397. {
  398. return i;
  399. }
  400. }
  401. return -1;
  402. }
  403. int PreferencesDialog::indexOfInteractiveWidget(
  404. QComboBox* interactiveWidgetList, InteractiveWidgetType widget)
  405. {
  406. for (int i = 0; interactiveWidgetList->count(); i++)
  407. {
  408. Q_ASSERT_X(
  409. interactiveWidgetList->itemData(i). canConvert<
  410. InteractiveWidgetListItemData> () == true,
  411. "PreferencesDialog::indexOfInteractiveWidget()",
  412. "Data extraction from QVariant failed !");
  413. InteractiveWidgetListItemData dataStruct =
  414. interactiveWidgetList->itemData(i).value<
  415. InteractiveWidgetListItemData> ();
  416. if (dataStruct.widget == widget)
  417. {
  418. return i;
  419. }
  420. }
  421. return -1;
  422. }