File: widgetexample.cpp

package info (click to toggle)
dtkwidget 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 36,540 kB
  • sloc: cpp: 63,257; ansic: 132; python: 88; sh: 42; makefile: 13
file content (217 lines) | stat: -rw-r--r-- 6,314 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "widgetexample.h"

#include <DCalendarWidget>
#include <dmpriscontrol.h>

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHeaderView>
#include <QTableView>
#include <QDate>
#include <QDebug>

WidgetExampleWindow::WidgetExampleWindow(QWidget *parent)
    : PageWindowInterface(parent)
{
    addExampleWindow(new DCalendarWidgetExample(this));
    addExampleWindow(new DTableWidgetExample(this));
    addExampleWindow(new DMPRISControlWidgetExample(this));
}

DCalendarWidgetExample::DCalendarWidgetExample(QWidget *parent)
    : ExampleWindowInterface(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    setLayout(mainLayout);

    DCalendarWidget *calender = new DCalendarWidget(this);
    QLabel *label = new QLabel(this);
    label->setPixmap(QPixmap("://images/example/DCalendarWidget.png"));
    label->setScaledContents(true);
    label->setFixedSize(550, 406);

    mainLayout->addWidget(calender, 0, Qt::AlignHCenter);
    mainLayout->addSpacing(50);
    mainLayout->addWidget(label, 0, Qt::AlignHCenter);
}

QString DCalendarWidgetExample::getTitleName() const
{
    return "DCalendarWidget";
}

QString DCalendarWidgetExample::getDescriptionInfo() const
{
    return "所涉及到日期操作的地方。";
}

int DCalendarWidgetExample::getFixedHeight() const
{
    return 816;
}

DTableWidgetExample::DTableWidgetExample(QWidget *parent)
    : ExampleWindowInterface(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    setLayout(mainLayout);

    tableView = new QTableView(this);
    CalendarModel *model = new CalendarModel(this);
    tableView->setModel(model);
    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tableView->setShowGrid(false);
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tableView->verticalHeader()->hide();
    tableView->horizontalHeader()->setSectionsClickable(false);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);

    tableView->setFixedSize(504, 252);
    mainLayout->addWidget(tableView, 0, Qt::AlignHCenter);
}

QString DTableWidgetExample::getTitleName() const
{
    return "DTableWidget";
}

QString DTableWidgetExample::getDescriptionInfo() const
{
    return "标准的表格控件。";
}

int DTableWidgetExample::getFixedHeight() const
{
    return 352;
}

CalendarModel::CalendarModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    header << "周日"
           << "周一"
           << "周二"
           << "周三"
           << "周四"
           << "周五"
           << "周六";

    //生成一个6行7列的数组
    for (int index = 0; index < 6; ++index) {
        m_tableData.push_back(QVector<QDate>(7));
    }

    const QDate curMonthFirstDay(QDate::currentDate().year(), QDate::currentDate().month(), 1);
    const int first_day_index = curMonthFirstDay.dayOfWeek() % 7;
    //本月第一天在数组中初始化
    m_tableData[0][first_day_index] = curMonthFirstDay;

    const int days_in_month = QDate::currentDate().daysInMonth();
    const QDate curMonthLastDay(QDate::currentDate().year(), QDate::currentDate().month(), days_in_month);

    const int last_day_index = first_day_index + days_in_month - 1;
    //本月最后一天在数组中初始化
    m_tableData[last_day_index / 7][last_day_index % 7] = curMonthLastDay;

    for (int rowIndex = 0; rowIndex < 6; ++rowIndex) {
        for (int colIndex = 0; colIndex < 7; ++colIndex) {
            if (m_tableData[rowIndex][colIndex].isValid()) {
                continue;
            }

            int index = rowIndex * 7 + colIndex;
            m_tableData[rowIndex][colIndex] = curMonthFirstDay.addDays(index - first_day_index);
        }
    }
}

int CalendarModel::rowCount(const QModelIndex &) const
{
    return m_tableData.size();
}

int CalendarModel::columnCount(const QModelIndex &) const
{
    return header.size();
}

QVariant CalendarModel::data(const QModelIndex &index, int role) const
{
    DPalette palette = DGuiApplicationHelper::instance()->applicationPalette();

    switch (role) {
    case Qt::DisplayRole: {
        int days = m_tableData[index.row()][index.column()].day();
        if (days == 1) {
            return QString("%1/1").arg(m_tableData[index.row()][index.column()].month());
        } else {
            return QString::number(days);
        }
    }
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    case Qt::TextColorRole: {
#else
    case Qt::ForegroundRole:{
#endif
        // 设置文字颜色
        if (m_tableData[index.row()][index.column()].month() == QDate::currentDate().month()) {
            return palette.color(DPalette::TextTitle);
        } else {
            return palette.color(DPalette::PlaceholderText);
        }
    }
    case Qt::TextAlignmentRole:
        return Qt::AlignCenter;
    case Qt::BackgroundRole:
        // 设置单元格背景色
        break;
    default:
        break;
    }
    return QVariant();
}

QVariant CalendarModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        role == Qt::BackgroundColorRole ||
#endif
        role == Qt::BackgroundRole)
        return DGuiApplicationHelper::instance()->applicationPalette().brush(DPalette::ItemBackground);
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        return header.value(section);
    } else {
        return QVariant();
    }
}

DMPRISControlWidgetExample::DMPRISControlWidgetExample(QWidget *parent)
    : ExampleWindowInterface(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    DMPRISControl *control = new DMPRISControl(this);
    layout->addWidget(control, 0, Qt::AlignCenter);
    control->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

QString DMPRISControlWidgetExample::getTitleName() const
{
    return QStringLiteral("DMPRISControl");
}

QString DMPRISControlWidgetExample::getDescriptionInfo() const
{
    return QStringLiteral("支持MPRIS协议的媒体控制控件。");
}

int DMPRISControlWidgetExample::getFixedHeight() const
{
    return 200;
}