PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/controllers/dlgprefcontrollers.cpp

https://gitlab.com/mixxxdj/mixxx
C++ | 164 lines | 130 code | 27 blank | 7 comment | 12 complexity | 1d7acb2ff461aa9a3adfb54529280599 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, BSD-3-Clause, LGPL-2.0, AGPL-1.0, LGPL-2.1
  1. #include "controllers/dlgprefcontrollers.h"
  2. #include <QDesktopServices>
  3. #include "controllers/controllermanager.h"
  4. #include "controllers/defs_controllers.h"
  5. #include "controllers/dlgprefcontroller.h"
  6. #include "defs_urls.h"
  7. #include "preferences/dialog/dlgpreferences.h"
  8. DlgPrefControllers::DlgPrefControllers(DlgPreferences* pPreferences,
  9. UserSettingsPointer pConfig,
  10. ControllerManager* pControllerManager,
  11. QTreeWidgetItem* pControllerTreeItem)
  12. : DlgPreferencePage(pPreferences),
  13. m_pDlgPreferences(pPreferences),
  14. m_pConfig(pConfig),
  15. m_pControllerManager(pControllerManager),
  16. m_pControllerTreeItem(pControllerTreeItem) {
  17. setupUi(this);
  18. setupControllerWidgets();
  19. const QString presetsPath = userPresetsPath(m_pConfig);
  20. connect(btnOpenUserPresets, &QPushButton::clicked,
  21. this, [this, presetsPath] { slotOpenLocalFile(presetsPath); });
  22. // Connections
  23. connect(m_pControllerManager, SIGNAL(devicesChanged()),
  24. this, SLOT(rescanControllers()));
  25. }
  26. DlgPrefControllers::~DlgPrefControllers() {
  27. destroyControllerWidgets();
  28. }
  29. void DlgPrefControllers::slotOpenLocalFile(const QString& file) {
  30. QDesktopServices::openUrl(QUrl::fromLocalFile(file));
  31. }
  32. void DlgPrefControllers::slotUpdate() {
  33. for (DlgPrefController* pControllerWindows : qAsConst(m_controllerWindows)) {
  34. pControllerWindows->slotUpdate();
  35. }
  36. }
  37. void DlgPrefControllers::slotCancel() {
  38. for (DlgPrefController* pControllerWindows : qAsConst(m_controllerWindows)) {
  39. pControllerWindows->slotCancel();
  40. }
  41. }
  42. void DlgPrefControllers::slotApply() {
  43. for (DlgPrefController* pControllerWindows : qAsConst(m_controllerWindows)) {
  44. pControllerWindows->slotApply();
  45. }
  46. }
  47. void DlgPrefControllers::slotResetToDefaults() {
  48. for (DlgPrefController* pControllerWindows : qAsConst(m_controllerWindows)) {
  49. pControllerWindows->slotResetToDefaults();
  50. }
  51. }
  52. QUrl DlgPrefControllers::helpUrl() const {
  53. return QUrl(MIXXX_MANUAL_CONTROLLERS_URL);
  54. }
  55. bool DlgPrefControllers::handleTreeItemClick(QTreeWidgetItem* clickedItem) {
  56. int controllerIndex = m_controllerTreeItems.indexOf(clickedItem);
  57. if (controllerIndex >= 0) {
  58. DlgPrefController* controllerWidget = m_controllerWindows.value(controllerIndex);
  59. if (controllerWidget) {
  60. m_pDlgPreferences->switchToPage(controllerWidget);
  61. }
  62. return true;
  63. } else if (clickedItem == m_pControllerTreeItem) {
  64. // Switch to the root page and expand the controllers tree item.
  65. m_pDlgPreferences->expandTreeItem(clickedItem);
  66. m_pDlgPreferences->switchToPage(this);
  67. return true;
  68. }
  69. return false;
  70. }
  71. void DlgPrefControllers::rescanControllers() {
  72. destroyControllerWidgets();
  73. setupControllerWidgets();
  74. }
  75. void DlgPrefControllers::destroyControllerWidgets() {
  76. while (!m_controllerWindows.isEmpty()) {
  77. DlgPrefController* controllerDlg = m_controllerWindows.takeLast();
  78. m_pDlgPreferences->removePageWidget(controllerDlg);
  79. delete controllerDlg;
  80. }
  81. m_controllerTreeItems.clear();
  82. while(m_pControllerTreeItem->childCount() > 0) {
  83. QTreeWidgetItem* controllerWindowLink = m_pControllerTreeItem->takeChild(0);
  84. delete controllerWindowLink;
  85. }
  86. }
  87. void DlgPrefControllers::setupControllerWidgets() {
  88. // For each controller, create a dialog and put a little link to it in the
  89. // treepane on the left.
  90. QList<Controller*> controllerList =
  91. m_pControllerManager->getControllerList(false, true);
  92. std::sort(controllerList.begin(), controllerList.end(), controllerCompare);
  93. foreach (Controller* pController, controllerList) {
  94. DlgPrefController* controllerDlg = new DlgPrefController(
  95. this, pController, m_pControllerManager, m_pConfig);
  96. connect(controllerDlg, SIGNAL(mappingStarted()),
  97. m_pDlgPreferences, SLOT(hide()));
  98. connect(controllerDlg, SIGNAL(mappingEnded()),
  99. m_pDlgPreferences, SLOT(show()));
  100. m_controllerWindows.append(controllerDlg);
  101. m_pDlgPreferences->addPageWidget(controllerDlg);
  102. connect(pController,
  103. &Controller::openChanged,
  104. [this, controllerDlg](bool bOpen) {
  105. slotHighlightDevice(controllerDlg, bOpen);
  106. });
  107. QTreeWidgetItem * controllerWindowLink = new QTreeWidgetItem(QTreeWidgetItem::Type);
  108. controllerWindowLink->setIcon(0, QIcon(":/images/preferences/ic_preferences_controllers.png"));
  109. QString curDeviceName = pController->getName();
  110. controllerWindowLink->setText(0, curDeviceName);
  111. controllerWindowLink->setTextAlignment(0, Qt::AlignLeft | Qt::AlignVCenter);
  112. controllerWindowLink->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  113. m_pControllerTreeItem->addChild(controllerWindowLink);
  114. m_controllerTreeItems.append(controllerWindowLink);
  115. // Set the font correctly
  116. QFont temp = controllerWindowLink->font(0);
  117. temp.setBold(pController->isOpen());
  118. controllerWindowLink->setFont(0, temp);
  119. }
  120. // If no controllers are available, show the "No controllers available"
  121. // message.
  122. txtNoControllersAvailable->setVisible(controllerList.empty());
  123. }
  124. void DlgPrefControllers::slotHighlightDevice(DlgPrefController* dialog, bool enabled) {
  125. int dialogIndex = m_controllerWindows.indexOf(dialog);
  126. if (dialogIndex < 0) {
  127. return;
  128. }
  129. QTreeWidgetItem * controllerWindowLink =
  130. m_controllerTreeItems.at(dialogIndex);
  131. if (!controllerWindowLink) {
  132. return;
  133. }
  134. QFont temp = controllerWindowLink->font(0);
  135. temp.setBold(enabled);
  136. controllerWindowLink->setFont(0,temp);
  137. }