BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ContentPane.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/AccordionWidget/ContentPane.cpp
6 //! @brief Implements ContentPane class
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 // This file is part of qAccordion. An Accordion widget for Qt
16 // Copyright (C) 2015 Christian Rapp <0x2a at posteo dot org>
17 //
18 // This program is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 // This program is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 // GNU General Public License for more details.
27 //
28 // You should have received a copy of the GNU General Public License
29 // along with this program. If not, see <http://www.gnu.org/licenses/>.
30 
32 
34 
35 ContentPane::ContentPane(QString header, QWidget* parent) : QWidget(parent)
36 {
37  this->content = nullptr;
38 
39  this->initDefaults(std::move(header));
40 }
41 
42 ContentPane::ContentPane(QString header, QFrame* content, QWidget* parent)
43  : QWidget(parent), content(content)
44 {
45  this->initDefaults(std::move(header));
46 }
47 
49 {
50  return this->active;
51 }
52 
54 {
55  return this->content;
56 }
57 
58 void ContentPane::setContentFrame(QFrame* content)
59 {
60  this->container->layout()->removeWidget(this->content);
61  if (this->content != nullptr)
62  delete (this->content);
63  this->content = content;
64  dynamic_cast<QVBoxLayout*>(this->container->layout())->insertWidget(0, this->content);
65 }
66 
68 {
69  return this->container->maximumHeight();
70 }
71 
72 void ContentPane::setMaximumHeight(int maxHeight)
73 {
74  this->containerAnimationMaxHeight = maxHeight;
75 
76  if (this->getActive())
77  this->container->setMaximumHeight(this->containerAnimationMaxHeight);
78  this->openAnimation->setEndValue(this->containerAnimationMaxHeight);
79  this->closeAnimation->setStartValue(this->containerAnimationMaxHeight);
80 }
81 
82 void ContentPane::setHeader(QString header)
83 {
84  this->header->setHeader(std::move(header));
85 }
86 
88 {
89  return this->header->getHeader();
90 }
91 
92 void ContentPane::setHeaderTooltip(QString tooltip)
93 {
94  this->header->setToolTip(tooltip);
95 }
96 
98 {
99  return this->header->toolTip();
100 }
101 
102 void ContentPane::setHeaderStylesheet(QString stylesheet)
103 {
104  this->header->setNormalStylesheet(std::move(stylesheet));
105 }
106 
108 {
109  return this->header->getNormalStylesheet();
110 }
111 
113 {
114  this->header->setHoverStylesheet(std::move(stylesheet));
115 }
116 
118 {
119  return this->header->getHoverStylesheet();
120 }
121 
123 {
124  this->header->setFrameStyle(style);
125 }
126 
128 {
129  return this->header->frameStyle();
130 }
131 
133 {
134  this->container->setFrameStyle(style);
135 }
136 
138 {
139  return this->container->frameStyle();
140 }
141 
143 {
144  if (this->getActive())
145  return;
146  this->openAnimation->start();
148  this->active = true;
149 }
150 
152 {
153  if (!this->getActive())
154  return;
155  this->closeAnimation->start();
157  this->active = false;
158 }
159 
160 void ContentPane::initDefaults(QString header)
161 {
162  this->active = false;
163 
164  this->headerFrameStyle = QFrame::Shape::StyledPanel | QFrame::Shadow::Raised;
165  this->contentPaneFrameStyle = QFrame::Shape::StyledPanel | QFrame::Shadow::Plain;
166  this->containerAnimationMaxHeight = 150;
167  // TODO: Why do I need to set the vertial policy to Maximum? from the api
168  // documentation Minimum would make more sens :/
169  this->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Maximum);
170 
171  this->setLayout(new QVBoxLayout());
172  this->layout()->setSpacing(0);
173  this->layout()->setContentsMargins(QMargins());
174 
175  this->initHeaderFrame(std::move(header));
177  this->initAnimations();
178 }
179 
180 void ContentPane::initHeaderFrame(QString header)
181 {
182  this->header = new ClickableFrame(std::move(header));
183  this->header->setFrameStyle(this->headerFrameStyle);
184  this->layout()->addWidget(this->header);
185 
186  QObject::connect(this->header, &ClickableFrame::singleClick, this, &ContentPane::headerClicked);
187 }
188 
190 {
191  this->container = new QFrame();
192  this->container->setLayout(new QVBoxLayout());
193  this->container->setFrameStyle(this->contentPaneFrameStyle);
194  this->container->setMaximumHeight(0);
195  this->container->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
196  this->layout()->addWidget(this->container);
197 
198  if (this->content == nullptr) {
199  this->content = new QFrame();
200  }
201 
202  this->container->layout()->addWidget(this->content);
203  this->container->layout()->setSpacing(0);
204  this->container->layout()->setContentsMargins(QMargins());
205 }
206 
208 {
209  this->openAnimation = std::unique_ptr<QPropertyAnimation>(new QPropertyAnimation());
210  this->closeAnimation = std::unique_ptr<QPropertyAnimation>(new QPropertyAnimation());
211  // TODO: Currently these animations only animate maximumHeight. This leads to
212  // different behaviour depending on whether the Accordion Widget is placed
213  // inside a QScollWidget or not. Maybe we also need to animate minimumHeight
214  // as well to get the same effect.
215  this->openAnimation->setTargetObject(this->container);
216  this->openAnimation->setPropertyName("maximumHeight");
217  this->closeAnimation->setTargetObject(this->container);
218  this->closeAnimation->setPropertyName("maximumHeight");
219 
220  this->openAnimation->setDuration(100);
221  this->closeAnimation->setDuration(100);
222  this->openAnimation->setStartValue(0);
223  this->closeAnimation->setStartValue(this->containerAnimationMaxHeight);
224  this->openAnimation->setEndValue(this->containerAnimationMaxHeight);
225  this->closeAnimation->setEndValue(0);
226  this->openAnimation->setEasingCurve(QEasingCurve(QEasingCurve::Type::Linear));
227  this->closeAnimation->setEasingCurve(QEasingCurve(QEasingCurve::Type::Linear));
228 }
229 
231 {
232  emit this->clicked();
233 }
234 
235 void ContentPane::paintEvent(ATTR_UNUSED QPaintEvent* event)
236 {
237  QStyleOption o;
238  o.initFrom(this);
239  QPainter p(this);
240  style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
241 }
#define ATTR_UNUSED
Defines ContentPane class.
The ClickableFrame class.
void setNormalStylesheet(QString stylesheet)
Set the default stylesheet.
QString getHoverStylesheet()
Get mouseover stylesheet.
void setHoverStylesheet(QString stylesheet)
Set mouseover stylesheet.
QString getHeader()
Get the header string.
void singleClick(QPoint pos)
Signal that is emitted upon a singleclick.
QString getNormalStylesheet()
Get the default stylesheet.
void setCaretPixmap(QString pixmapPath)
Set the caret pixmap.
void setHeaderTooltip(QString tooltip)
Set header tooltip.
Definition: ContentPane.cpp:92
void clicked()
Clicked signal is emitted when the header is clicked.
QString getHeaderStylesheet()
Get the current header style sheet.
ClickableFrame * header
Definition: ContentPane.h:284
void setHeaderStylesheet(QString stylesheet)
Set a stylesheet for the header frame.
QString getHeaderTooltip()
Get the header tooltip.
Definition: ContentPane.cpp:97
int getMaximumHeight()
Get the maximum height of the content pane container frame.
Definition: ContentPane.cpp:67
int containerAnimationMaxHeight
Definition: ContentPane.h:289
void closeContentPane()
Close the content pane.
ContentPane(QString header, QWidget *parent=0)
ContentPane constructor.
Definition: ContentPane.cpp:35
QString getHeader()
Return the header of the content pane.
Definition: ContentPane.cpp:87
int contentPaneFrameStyle
Definition: ContentPane.h:288
QFrame * getContentFrame()
Get the content frame of the content pane.
Definition: ContentPane.cpp:53
void initAnimations()
void openContentPane()
Open the content pane.
std::unique_ptr< QPropertyAnimation > openAnimation
Definition: ContentPane.h:293
bool getActive()
Check if this Content pane is active.
Definition: ContentPane.cpp:48
QFrame * content
Definition: ContentPane.h:283
QString getHeaderHoverStylesheet()
Get the mouse over header style sheet.
void initHeaderFrame(QString header)
void setContentFrame(QFrame *content)
Set the content frame.
Definition: ContentPane.cpp:58
void setHeaderFrameStyle(int style)
Set the header frame style.
int getContainerFrameStyle()
Get the container frame style.
void setHeader(QString header)
Set the header of the content pane.
Definition: ContentPane.cpp:82
void setMaximumHeight(int maxHeight)
Set the maximum height of the content pane container.
Definition: ContentPane.cpp:72
QFrame * container
Definition: ContentPane.h:285
void headerClicked()
Slot that is called when the header has been triggered.
void initDefaults(QString header)
void setHeaderHoverStylesheet(QString stylesheet)
Set a stylesheet for the header frame when the mouse hovers over it.
void setContainerFrameStyle(int style)
Set the container frame style.
int getHeaderFrameStyle()
Get the header frame style.
std::unique_ptr< QPropertyAnimation > closeAnimation
Definition: ContentPane.h:294
int headerFrameStyle
Definition: ContentPane.h:287
void initContainerContentFrame()
void paintEvent(ATTR_UNUSED QPaintEvent *event)
paintEvent Reimplement paintEvent to use stylesheets in derived Widgets
const char *const CARRET_ICON_OPENED
Qt qrc "path" for the opened icon.
const char *const CARRET_ICON_CLOSED
Qt qrc "path" for the closed icon.