forked from martijnkoopman/Qt-Ribbon-Widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ribbontabcontent.cpp
116 lines (102 loc) · 2.93 KB
/
ribbontabcontent.cpp
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
/*
* Copyright (C) Martijn Koopman
* All Rights Reserved
*
* This software is distributed WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
*/
#include "ribbontabcontent.h"
#include "ui_ribbontabcontent.h"
#include "ribbonbuttongroup.h"
RibbonTabContent::RibbonTabContent(QWidget *parent)
: QWidget(parent)
, ui(new Ui::RibbonTabContent)
{
ui->setupUi(this);
}
RibbonTabContent::~RibbonTabContent()
{
delete ui;
}
void RibbonTabContent::addGroup(const QString &groupName)
{
RibbonButtonGroup *ribbonButtonGroup = new RibbonButtonGroup;
ribbonButtonGroup->setTitle(groupName);
ui->ribbonHorizontalLayout->addWidget(ribbonButtonGroup);
}
void RibbonTabContent::removeGroup(const QString &groupName)
{
// Find ribbon group
for (int i = 0; i < ui->ribbonHorizontalLayout->count(); i++)
{
RibbonButtonGroup *group = static_cast<RibbonButtonGroup*>(ui->ribbonHorizontalLayout->itemAt(i)->widget());
if (group->title().toLower() == groupName.toLower())
{
ui->ribbonHorizontalLayout->removeWidget(group); /// \todo :( No effect
delete group;
break;
}
}
/// \todo What if the group still contains buttons? Delete manually?
// Or automaticly deleted by Qt parent() system.
}
int RibbonTabContent::groupCount() const
{
return ui->ribbonHorizontalLayout->count();
}
void RibbonTabContent::addButton(const QString &groupName, QToolButton *button)
{
// Find ribbon group
RibbonButtonGroup *ribbonButtonGroup = nullptr;
for (int i = 0; i < ui->ribbonHorizontalLayout->count(); i++)
{
RibbonButtonGroup *group = static_cast<RibbonButtonGroup*>(ui->ribbonHorizontalLayout->itemAt(i)->widget());
if (group->title().toLower() == groupName.toLower())
{
ribbonButtonGroup = group;
break;
}
}
if (ribbonButtonGroup != nullptr)
{
// Group found
// Add ribbon button
ribbonButtonGroup->addButton(button);
}
else
{
// Group not found
// Add ribbon group
addGroup(groupName);
// Add ribbon button
addButton(groupName, button);
}
}
void RibbonTabContent::removeButton(const QString &groupName, QToolButton *button)
{
// Find ribbon group
RibbonButtonGroup *ribbonButtonGroup = nullptr;
for (int i = 0; i < ui->ribbonHorizontalLayout->count(); i++)
{
RibbonButtonGroup *group = static_cast<RibbonButtonGroup*>(ui->ribbonHorizontalLayout->itemAt(i)->widget());
if (group->title().toLower() == groupName.toLower())
{
ribbonButtonGroup = group;
break;
}
}
if (ribbonButtonGroup != nullptr)
{
// Group found
// Remove ribbon button
ribbonButtonGroup->removeButton(button);
if (ribbonButtonGroup->buttonCount() == 0)
{
// Empty button group
// Remove button group
removeGroup(groupName);
}
}
}