File: ClickableFrame.cpp

package info (click to toggle)
bornagain 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,800 kB
  • sloc: cpp: 469,684; python: 38,920; xml: 805; awk: 630; sh: 286; ansic: 37; makefile: 25
file content (139 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// ************************************************************************** //
//
//  BornAgain: simulate and fit scattering at grazing incidence
//
//! @file      GUI/coregui/Views/AccordionWidget/ClickableFrame.cpp
//! @brief     Implements AccordionWidget class
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************** //

// This file is part of qAccordion. An Accordion widget for Qt
// Copyright (C) 2015 Christian Rapp <0x2a at posteo dot org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "GUI/coregui/Views/AccordionWidget/ClickableFrame.h"
#include <QStyleOption>

ClickableFrame::ClickableFrame(QString header, QWidget* parent, Qt::WindowFlags f)
    : QFrame(parent, f), header(header)
{
    this->setAttribute(Qt::WA_Hover, true);
    this->clickable = true;
    this->setCursor(Qt::PointingHandCursor);
    QColor background = this->palette().color(QPalette::ColorRole::Background);
    QColor lighter = background.lighter(110);
    this->normalStylesheet = "";
    this->hoverStylesheet = "QFrame {background-color: " + lighter.name() + ";}";
    this->initFrame();
}

void ClickableFrame::setClickable(bool status)
{
    this->clickable = status;
    if (status) {
        this->setCursor(Qt::PointingHandCursor);
    } else {
        this->setCursor(Qt::ForbiddenCursor);
    }
}

bool ClickableFrame::getClickable()
{
    return this->clickable;
}

void ClickableFrame::setHeader(QString header)
{
    this->header = header;
    this->nameLabel->setText(this->header);
}

QString ClickableFrame::getHeader()
{
    return this->header;
}

void ClickableFrame::setNormalStylesheet(QString stylesheet)
{
    this->normalStylesheet = stylesheet;
    this->setStyleSheet(this->normalStylesheet);
}

QString ClickableFrame::getNormalStylesheet()
{
    return this->normalStylesheet;
}

void ClickableFrame::setHoverStylesheet(QString stylesheet)
{
    this->hoverStylesheet = stylesheet;
}

QString ClickableFrame::getHoverStylesheet()
{
    return this->hoverStylesheet;
}

void ClickableFrame::setCaretPixmap(QString pixmapPath)
{
    this->caretLabel->setPixmap(QPixmap(pixmapPath));
}

void ClickableFrame::initFrame()
{
    this->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Fixed);
    this->setLayout(new QHBoxLayout());

    this->caretLabel = new QLabel();
    this->caretLabel->setPixmap(QPixmap(":/qAccordionIcons/caret-right.png"));
    this->layout()->addWidget(this->caretLabel);

    this->nameLabel = new QLabel();
    nameLabel->setText(this->header);
    this->layout()->addWidget(nameLabel);

    dynamic_cast<QHBoxLayout*>(this->layout())->addStretch();

    this->setStyleSheet(this->normalStylesheet);
}

void ClickableFrame::mousePressEvent(QMouseEvent* event)
{
    if (this->clickable) {
        emit this->singleClick(event->pos());
        event->accept();
    } else {
        event->ignore();
    }
}

void ClickableFrame::enterEvent(ATTR_UNUSED QEvent* event)
{
    if (this->clickable) {
        this->setStyleSheet(this->hoverStylesheet);
    }
}

void ClickableFrame::leaveEvent(ATTR_UNUSED QEvent* event)
{
    if (this->clickable) {
        this->setStyleSheet(this->normalStylesheet);
    }
}