Row.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /******************************************************************************
  2. Copyright (C) 2023 by Dennis Sädtler <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <Idian/Row.hpp>
  15. #include <QApplication>
  16. #include <QComboBox>
  17. #include <QSizePolicy>
  18. #include <QSvgRenderer>
  19. #include <Idian/moc_Row.cpp>
  20. namespace idian {
  21. Row::Row(QWidget *parent) : GenericRow(parent)
  22. {
  23. layout = new QGridLayout(this);
  24. layout->setVerticalSpacing(0);
  25. layout->setContentsMargins(0, 0, 0, 0);
  26. labelLayout = new QVBoxLayout();
  27. labelLayout->setSpacing(0);
  28. labelLayout->setContentsMargins(0, 0, 0, 0);
  29. setFocusPolicy(Qt::StrongFocus);
  30. setLayout(layout);
  31. layout->setColumnMinimumWidth(0, 0);
  32. layout->setColumnStretch(0, 0);
  33. layout->setColumnStretch(1, 40);
  34. layout->setColumnStretch(2, 55);
  35. nameLabel = new QLabel();
  36. nameLabel->setVisible(false);
  37. Utils::addClass(nameLabel, "title");
  38. descriptionLabel = new QLabel();
  39. descriptionLabel->setVisible(false);
  40. Utils::addClass(descriptionLabel, "description");
  41. labelLayout->addWidget(nameLabel);
  42. labelLayout->addWidget(descriptionLabel);
  43. layout->addLayout(labelLayout, 0, 1, Qt::AlignLeft);
  44. }
  45. void Row::setPrefix(QWidget *w, bool auto_connect)
  46. {
  47. setSuffixEnabled(false);
  48. prefix_ = w;
  49. if (auto_connect)
  50. this->connectBuddyWidget(w);
  51. prefix_->setParent(this);
  52. layout->addWidget(prefix_, 0, 0, Qt::AlignLeft);
  53. layout->setColumnStretch(0, 3);
  54. }
  55. void Row::setSuffix(QWidget *w, bool auto_connect)
  56. {
  57. setPrefixEnabled(false);
  58. suffix_ = w;
  59. if (auto_connect)
  60. this->connectBuddyWidget(w);
  61. suffix_->setParent(this);
  62. layout->addWidget(suffix_, 0, 2, Qt::AlignRight | Qt::AlignVCenter);
  63. }
  64. void Row::setPrefixEnabled(bool enabled)
  65. {
  66. if (!prefix_)
  67. return;
  68. if (enabled)
  69. setSuffixEnabled(false);
  70. if (enabled == prefix_->isEnabled() && enabled == prefix_->isVisible())
  71. return;
  72. layout->setColumnStretch(0, enabled ? 3 : 0);
  73. prefix_->setEnabled(enabled);
  74. prefix_->setVisible(enabled);
  75. }
  76. void Row::setSuffixEnabled(bool enabled)
  77. {
  78. if (!suffix_)
  79. return;
  80. if (enabled)
  81. setPrefixEnabled(false);
  82. if (enabled == suffix_->isEnabled() && enabled == suffix_->isVisible())
  83. return;
  84. suffix_->setEnabled(enabled);
  85. suffix_->setVisible(enabled);
  86. }
  87. void Row::setTitle(const QString &name)
  88. {
  89. nameLabel->setText(name);
  90. setAccessibleName(name);
  91. showTitle(true);
  92. }
  93. void Row::setDescription(const QString &description)
  94. {
  95. descriptionLabel->setText(description);
  96. setAccessibleDescription(description);
  97. showDescription(true);
  98. }
  99. void Row::showTitle(bool visible)
  100. {
  101. nameLabel->setVisible(visible);
  102. }
  103. void Row::showDescription(bool visible)
  104. {
  105. descriptionLabel->setVisible(visible);
  106. }
  107. void Row::setBuddy(QWidget *widget)
  108. {
  109. buddyWidget = widget;
  110. Utils::addClass(widget, "row-buddy");
  111. }
  112. void Row::setChangeCursor(bool change)
  113. {
  114. changeCursor = change;
  115. Utils::toggleClass("cursor-pointer", change);
  116. }
  117. void Row::enterEvent(QEnterEvent *event)
  118. {
  119. if (!isEnabled())
  120. return;
  121. if (changeCursor) {
  122. setCursor(Qt::PointingHandCursor);
  123. }
  124. Utils::addClass("hover");
  125. if (buddyWidget)
  126. Utils::repolish(buddyWidget);
  127. if (hasPrefix() || hasSuffix()) {
  128. Utils::polishChildren();
  129. }
  130. GenericRow::enterEvent(event);
  131. }
  132. void Row::leaveEvent(QEvent *event)
  133. {
  134. Utils::removeClass("hover");
  135. if (buddyWidget)
  136. Utils::repolish(buddyWidget);
  137. if (hasPrefix() || hasSuffix()) {
  138. Utils::polishChildren();
  139. }
  140. GenericRow::leaveEvent(event);
  141. }
  142. void Row::mouseReleaseEvent(QMouseEvent *event)
  143. {
  144. if (event->button() & Qt::LeftButton) {
  145. emit clicked();
  146. }
  147. QFrame::mouseReleaseEvent(event);
  148. }
  149. void Row::keyReleaseEvent(QKeyEvent *event)
  150. {
  151. if (event->key() == Qt::Key_Space || event->key() == Qt::Key_Enter) {
  152. emit clicked();
  153. }
  154. QFrame::keyReleaseEvent(event);
  155. }
  156. void Row::connectBuddyWidget(QWidget *widget)
  157. {
  158. setAccessibleName(nameLabel->text());
  159. setFocusProxy(widget);
  160. setBuddy(widget);
  161. // If element is a ToggleSwitch and checkable, forward clicks to the widget
  162. ToggleSwitch *obsToggle = dynamic_cast<ToggleSwitch *>(widget);
  163. if (obsToggle && obsToggle->isCheckable()) {
  164. setChangeCursor(true);
  165. connect(this, &Row::clicked, obsToggle, &ToggleSwitch::click);
  166. return;
  167. }
  168. // If element is any other QAbstractButton subclass, and checkable, forward clicks to the widget.
  169. QAbstractButton *button = dynamic_cast<QAbstractButton *>(widget);
  170. if (button && button->isCheckable()) {
  171. setChangeCursor(true);
  172. connect(this, &Row::clicked, button, &QAbstractButton::click);
  173. return;
  174. }
  175. // If element is an ComboBox, clicks toggle the dropdown.
  176. ComboBox *obsCombo = dynamic_cast<ComboBox *>(widget);
  177. if (obsCombo) {
  178. setChangeCursor(true);
  179. connect(this, &Row::clicked, obsCombo, &ComboBox::togglePopup);
  180. return;
  181. }
  182. }
  183. // Button for expanding a collapsible ActionRow
  184. ExpandButton::ExpandButton(QWidget *parent) : QAbstractButton(parent), Utils(this)
  185. {
  186. setCheckable(true);
  187. }
  188. void ExpandButton::paintEvent(QPaintEvent *)
  189. {
  190. QStyleOptionButton opt;
  191. opt.initFrom(this);
  192. QPainter p(this);
  193. bool checked = isChecked();
  194. opt.state.setFlag(QStyle::State_On, checked);
  195. opt.state.setFlag(QStyle::State_Off, !checked);
  196. opt.state.setFlag(QStyle::State_Sunken, checked);
  197. p.setRenderHint(QPainter::Antialiasing, true);
  198. p.setRenderHint(QPainter::SmoothPixmapTransform, true);
  199. style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &opt, &p, this);
  200. style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &p, this);
  201. }
  202. // Row variant that can be expanded to show another properties list
  203. CollapsibleRow::CollapsibleRow(QWidget *parent) : GenericRow(parent)
  204. {
  205. layout = new QVBoxLayout;
  206. layout->setContentsMargins(0, 0, 0, 0);
  207. layout->setSpacing(0);
  208. setLayout(layout);
  209. rowWidget = new RowFrame();
  210. rowLayout = new QHBoxLayout();
  211. rowLayout->setContentsMargins(0, 0, 0, 0);
  212. rowLayout->setSpacing(0);
  213. rowWidget->setLayout(rowLayout);
  214. actionRow = new Row();
  215. actionRow->setChangeCursor(false);
  216. rowLayout->addWidget(actionRow);
  217. propertyList = new PropertiesList(this);
  218. propertyList->setVisible(false);
  219. expandFrame = new QFrame();
  220. btnLayout = new QHBoxLayout();
  221. btnLayout->setContentsMargins(0, 0, 0, 0);
  222. btnLayout->setSpacing(0);
  223. expandFrame->setLayout(btnLayout);
  224. Utils::addClass(expandFrame, "btn-frame");
  225. actionRow->setBuddy(expandFrame);
  226. expandButton = new ExpandButton(this);
  227. btnLayout->addWidget(expandButton);
  228. rowLayout->addWidget(expandFrame);
  229. layout->addWidget(rowWidget);
  230. layout->addWidget(propertyList);
  231. actionRow->setFocusProxy(expandButton);
  232. connect(expandButton, &QAbstractButton::clicked, this, &CollapsibleRow::toggleVisibility);
  233. connect(actionRow, &Row::clicked, expandButton, &QAbstractButton::click);
  234. }
  235. void CollapsibleRow::setCheckable(bool check)
  236. {
  237. checkable = check;
  238. if (checkable && !toggleSwitch) {
  239. propertyList->setEnabled(false);
  240. Utils::polishChildren(propertyList);
  241. toggleSwitch = new ToggleSwitch(false);
  242. actionRow->setSuffix(toggleSwitch, false);
  243. connect(toggleSwitch, &ToggleSwitch::toggled, propertyList, &PropertiesList::setEnabled);
  244. connect(toggleSwitch, &ToggleSwitch::toggled, this, &CollapsibleRow::toggled);
  245. }
  246. if (!checkable && toggleSwitch) {
  247. propertyList->setEnabled(true);
  248. Utils::polishChildren(propertyList);
  249. actionRow->suffix()->deleteLater();
  250. }
  251. }
  252. void CollapsibleRow::setChecked(bool checked)
  253. {
  254. if (!isCheckable()) {
  255. throw std::logic_error("Called setChecked on a non-checkable row.");
  256. }
  257. toggleSwitch->setChecked(checked);
  258. }
  259. void CollapsibleRow::setTitle(const QString &name)
  260. {
  261. actionRow->setTitle(name);
  262. }
  263. void CollapsibleRow::setDescription(const QString &description)
  264. {
  265. actionRow->setDescription(description);
  266. }
  267. void CollapsibleRow::toggleVisibility()
  268. {
  269. bool visible = !propertyList->isVisible();
  270. propertyList->setVisible(visible);
  271. expandButton->setChecked(visible);
  272. }
  273. void CollapsibleRow::addRow(GenericRow *actionRow)
  274. {
  275. propertyList->addRow(actionRow);
  276. }
  277. RowFrame::RowFrame(QWidget *parent) : QFrame(parent), Utils(this) {}
  278. void RowFrame::enterEvent(QEnterEvent *event)
  279. {
  280. setCursor(Qt::PointingHandCursor);
  281. Utils::addClass("hover");
  282. Utils::polishChildren();
  283. QWidget::enterEvent(event);
  284. }
  285. void RowFrame::leaveEvent(QEvent *event)
  286. {
  287. Utils::removeClass("hover");
  288. Utils::polishChildren();
  289. QWidget::leaveEvent(event);
  290. }
  291. } // namespace idian