Row.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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(QString name)
  88. {
  89. nameLabel->setText(name);
  90. setAccessibleName(name);
  91. showTitle(true);
  92. }
  93. void Row::setDescription(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(const QString &name, 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->setTitle(name);
  216. actionRow->setChangeCursor(false);
  217. rowLayout->addWidget(actionRow);
  218. propertyList = new PropertiesList(this);
  219. propertyList->setVisible(false);
  220. expandFrame = new QFrame();
  221. btnLayout = new QHBoxLayout();
  222. btnLayout->setContentsMargins(0, 0, 0, 0);
  223. btnLayout->setSpacing(0);
  224. expandFrame->setLayout(btnLayout);
  225. Utils::addClass(expandFrame, "btn-frame");
  226. actionRow->setBuddy(expandFrame);
  227. expandButton = new ExpandButton(this);
  228. btnLayout->addWidget(expandButton);
  229. rowLayout->addWidget(expandFrame);
  230. layout->addWidget(rowWidget);
  231. layout->addWidget(propertyList);
  232. actionRow->setFocusProxy(expandButton);
  233. connect(expandButton, &QAbstractButton::clicked, this, &CollapsibleRow::toggleVisibility);
  234. connect(actionRow, &Row::clicked, expandButton, &QAbstractButton::click);
  235. }
  236. CollapsibleRow::CollapsibleRow(const QString &name, const QString &desc, QWidget *parent) : CollapsibleRow(name, parent)
  237. {
  238. actionRow->setDescription(desc);
  239. }
  240. void CollapsibleRow::setCheckable(bool check)
  241. {
  242. checkable = check;
  243. if (checkable && !toggleSwitch) {
  244. propertyList->setEnabled(false);
  245. Utils::polishChildren(propertyList);
  246. toggleSwitch = new ToggleSwitch(false);
  247. actionRow->setSuffix(toggleSwitch, false);
  248. connect(toggleSwitch, &ToggleSwitch::toggled, propertyList, &PropertiesList::setEnabled);
  249. }
  250. if (!checkable && toggleSwitch) {
  251. propertyList->setEnabled(true);
  252. Utils::polishChildren(propertyList);
  253. actionRow->suffix()->deleteLater();
  254. }
  255. }
  256. void CollapsibleRow::toggleVisibility()
  257. {
  258. bool visible = !propertyList->isVisible();
  259. propertyList->setVisible(visible);
  260. expandButton->setChecked(visible);
  261. }
  262. void CollapsibleRow::addRow(GenericRow *actionRow)
  263. {
  264. propertyList->addRow(actionRow);
  265. }
  266. RowFrame::RowFrame(QWidget *parent) : QFrame(parent), Utils(this) {}
  267. void RowFrame::enterEvent(QEnterEvent *event)
  268. {
  269. setCursor(Qt::PointingHandCursor);
  270. Utils::addClass("hover");
  271. Utils::polishChildren();
  272. QWidget::enterEvent(event);
  273. }
  274. void RowFrame::leaveEvent(QEvent *event)
  275. {
  276. Utils::removeClass("hover");
  277. Utils::polishChildren();
  278. QWidget::leaveEvent(event);
  279. }
  280. } // namespace idian