ctkPopupWidgetTest1.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QComboBox>
  17. #include <QHBoxLayout>
  18. #include <QMenu>
  19. #include <QPushButton>
  20. #include <QSlider>
  21. #include <QTimer>
  22. #include <QToolButton>
  23. #include <QVBoxLayout>
  24. // CTK includes
  25. #include "ctkCallback.h"
  26. #include "ctkCollapsibleButton.h"
  27. #include "ctkPopupWidget.h"
  28. // STD includes
  29. #include <cstdlib>
  30. #include <iostream>
  31. //-----------------------------------------------------------------------------
  32. QWidget* createPanel(const QString& title, QList<ctkPopupWidget*>& popups)
  33. {
  34. QWidget* topLevel = new QWidget(0);
  35. topLevel->setObjectName("topLevelWidget");
  36. topLevel->setWindowTitle(title);
  37. ctkCollapsibleButton* button = new ctkCollapsibleButton;
  38. QComboBox* focusComboBox = new QComboBox;
  39. focusComboBox->setObjectName("focusComboBox");
  40. focusComboBox->addItem("Focus popup");
  41. focusComboBox->addItem("Focus popup");
  42. focusComboBox->addItem("Focus popup");
  43. focusComboBox->addItem("Focus popup");
  44. QPushButton* openButton = new QPushButton("Open popup");
  45. openButton->setObjectName("openButton");
  46. QPushButton* toggleButton = new QPushButton("Toggle popup");
  47. toggleButton->setObjectName("toggleButton");
  48. toggleButton->setCheckable(true);
  49. QToolButton* pinButton = new QToolButton(0);
  50. pinButton->setCheckable(true);
  51. QVBoxLayout* collapsibleLayout = new QVBoxLayout;
  52. collapsibleLayout->addWidget(focusComboBox);
  53. button->setLayout(collapsibleLayout);
  54. QVBoxLayout* vlayout = new QVBoxLayout;
  55. vlayout->addWidget(button);
  56. vlayout->addWidget(openButton);
  57. vlayout->addWidget(toggleButton);
  58. vlayout->addWidget(pinButton);
  59. topLevel->setLayout(vlayout);
  60. ctkPopupWidget* focusPopup = new ctkPopupWidget(focusComboBox);
  61. focusPopup->setObjectName("focusPopup");
  62. focusPopup->setAutoShow(true);
  63. focusPopup->setAutoHide(true);
  64. QPushButton* focusPopupContent = new QPushButton("button");
  65. focusPopupContent->setObjectName("focusPopupContent");
  66. QToolButton* popupToolButton = new QToolButton;
  67. popupToolButton->setObjectName("popupToolButton");
  68. QMenu* menu = new QMenu(popupToolButton);
  69. menu->setObjectName("menu");
  70. menu->addAction("first menu item");
  71. menu->addAction("second menu item");
  72. menu->addAction("third menu item");
  73. menu->addAction("fourth menu item");
  74. popupToolButton->setPopupMode(QToolButton::InstantPopup);
  75. popupToolButton->setMenu(menu);
  76. QHBoxLayout* focusLayout = new QHBoxLayout;
  77. focusLayout->addWidget(focusPopupContent);
  78. focusLayout->addWidget(popupToolButton);
  79. focusPopup->setLayout(focusLayout);
  80. focusLayout->setContentsMargins(0,0,0,0);
  81. QPalette palette = focusPopup->palette();
  82. QLinearGradient linearGradient(QPointF(0.f, 0.f), QPointF(0.f, 0.666f));
  83. linearGradient.setSpread(QGradient::PadSpread);
  84. linearGradient.setCoordinateMode(QGradient::StretchToDeviceMode);
  85. linearGradient.setColorAt(0, palette.color(QPalette::Window));
  86. linearGradient.setColorAt(1, palette.color(QPalette::Dark));
  87. palette.setBrush(QPalette::Window, linearGradient);
  88. focusPopup->setPalette(palette);
  89. ctkPopupWidget* openPopup = new ctkPopupWidget(openButton);
  90. openPopup->setObjectName("openPopup");
  91. openPopup->setFrameStyle(QFrame::Box);
  92. openPopup->setLineWidth(1);
  93. openPopup->setAutoShow(false);
  94. openPopup->setAutoHide(false);
  95. openPopup->setWindowOpacity(0.7);
  96. QPushButton* openPopupContent = new QPushButton("Close popup");
  97. openPopupContent->setObjectName("openPopupContent");
  98. QVBoxLayout* openLayout = new QVBoxLayout;
  99. openLayout->addWidget(openPopupContent);
  100. openPopup->setLayout(openLayout);
  101. QObject::connect(openButton, SIGNAL(clicked()),
  102. openPopup, SLOT(showPopup()));
  103. QObject::connect(openPopupContent, SIGNAL(clicked()),
  104. openPopup, SLOT(hidePopup()));
  105. ctkPopupWidget* togglePopup = new ctkPopupWidget(toggleButton);
  106. togglePopup->setObjectName("togglePopup");
  107. togglePopup->setAutoShow(false);
  108. togglePopup->setAutoHide(false);
  109. QPushButton* togglePopupContent = new QPushButton("tooltip button");
  110. togglePopupContent->setObjectName("togglePopupContent");
  111. togglePopupContent->setToolTip("tooltip");
  112. QVBoxLayout* toggleLayout = new QVBoxLayout;
  113. toggleLayout->addWidget(togglePopupContent);
  114. togglePopup->setLayout(toggleLayout);
  115. QObject::connect(toggleButton, SIGNAL(toggled(bool)),
  116. togglePopup, SLOT(showPopup(bool)));
  117. togglePopup->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  118. ctkPopupWidget* pinPopup = new ctkPopupWidget(pinButton);
  119. pinPopup->setObjectName("pinPopup");
  120. QPushButton* pinPopupContent = new QPushButton("pin button");
  121. pinPopupContent->setCheckable(true);
  122. QObject::connect(pinPopupContent, SIGNAL(toggled(bool)),
  123. pinButton, SLOT(setChecked(bool)));
  124. QObject::connect(pinButton, SIGNAL(toggled(bool)),
  125. pinPopupContent, SLOT(setChecked(bool)));
  126. pinPopupContent->setObjectName("pinPopupContent");
  127. QVBoxLayout* pinLayout = new QVBoxLayout;
  128. pinLayout->addWidget(pinPopupContent);
  129. pinPopup->setLayout(pinLayout);
  130. QObject::connect(pinButton, SIGNAL(toggled(bool)),
  131. pinPopup, SLOT(pinPopup(bool)));
  132. popups << focusPopup << openPopup << togglePopup << pinPopup;
  133. return topLevel;
  134. }
  135. //-----------------------------------------------------------------------------
  136. int ctkPopupWidgetTest1(int argc, char * argv [] )
  137. {
  138. QApplication app(argc, argv);
  139. QPushButton base("Top level push button");
  140. ctkPopupWidget popup(&base);
  141. QPushButton popupContent("popup");
  142. QVBoxLayout* layout = new QVBoxLayout;
  143. layout->addWidget(&popupContent);
  144. popup.setLayout(layout);
  145. popup.setAlignment(Qt::AlignVCenter | Qt::AlignRight);
  146. popup.setHorizontalDirection(Qt::RightToLeft);
  147. popup.setOrientation(Qt::Horizontal);
  148. base.show();
  149. QList<ctkPopupWidget*> popups;
  150. createPanel("Hidden", popups); //create a panel that is hidden (never shown)
  151. QWidget* scrollPanel = createPanel("Scroll", popups);
  152. foreach(ctkPopupWidget* popup, popups)
  153. {
  154. popup->setVerticalDirection(ctkPopupWidget::TopToBottom);
  155. popup->setHorizontalDirection(Qt::LeftToRight);
  156. popup->setAlignment( Qt::AlignBottom | Qt::AlignJustify);
  157. popup->setEasingCurve(QEasingCurve::OutElastic);
  158. QPalette p = popup->palette();
  159. p.setColor(QPalette::Window, QColor(255, 0, 0, 128));
  160. popup->setPalette(p);
  161. popup->setAttribute(Qt::WA_TranslucentBackground);
  162. }
  163. popups.clear();
  164. QWidget* fadePanel = createPanel("Window opacity", popups);
  165. foreach(ctkPopupWidget* popup, popups)
  166. {
  167. popup->setAnimationEffect(ctkPopupWidget::WindowOpacityFadeEffect);
  168. }
  169. scrollPanel->show();
  170. fadePanel->show();
  171. ctkPopupWidget screenPopup;
  172. screenPopup.setAutoHide(false);
  173. screenPopup.setOrientation(Qt::Horizontal | Qt::Vertical);
  174. QFrame screenPopupContents;
  175. screenPopupContents.setFixedSize(200, 200);
  176. QVBoxLayout* screenLayout = new QVBoxLayout;
  177. screenLayout->addWidget(&screenPopupContents);
  178. screenPopup.setLayout(screenLayout);
  179. screenPopup.move(0,0);
  180. QTimer::singleShot(200, &screenPopup, SLOT(showPopup()));
  181. if (argc < 2 || QString(argv[1]) != "-I" )
  182. {
  183. QTimer::singleShot(200, &app, SLOT(quit()));
  184. }
  185. return app.exec();
  186. }