Eventviews

timelabelszone.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno@virlet.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5*/
6#include "timelabelszone.h"
7#include "agenda.h"
8#include "agendaview.h"
9#include "prefs.h"
10#include "timelabels.h"
11
12#include <QHBoxLayout>
13#include <QScrollArea>
14#include <QScrollBar>
15
16using namespace EventViews;
17
18TimeLabelsZone::TimeLabelsZone(QWidget *parent, const PrefsPtr &preferences, Agenda *agenda)
19 : QWidget(parent)
20 , mAgenda(agenda)
21 , mPrefs(preferences)
22 , mParent(qobject_cast<AgendaView *>(parent))
23{
24 mTimeLabelsLayout = new QHBoxLayout(this);
25 mTimeLabelsLayout->setContentsMargins(0, 0, 0, 0);
26 mTimeLabelsLayout->setSpacing(0);
27
28 init();
29}
30
31void TimeLabelsZone::reset()
32{
33 for (QScrollArea *label : std::as_const(mTimeLabelsList)) {
34 label->hide();
35 label->deleteLater();
36 }
37 mTimeLabelsList.clear();
38
39 init();
40
41 // Update some related geometry from the agenda view
42 updateAll();
43 if (mParent) {
44 mParent->updateTimeBarWidth();
45 mParent->createDayLabels(true);
46 }
47}
48
49void TimeLabelsZone::init()
50{
51 QStringList seenTimeZones(QString::fromUtf8(mPrefs->timeZone().id()));
52
53 addTimeLabels(mPrefs->timeZone());
54
55 const auto lst = mPrefs->timeScaleTimezones();
56 for (const QString &zoneStr : lst) {
57 if (!seenTimeZones.contains(zoneStr)) {
58 auto zone = QTimeZone(zoneStr.toUtf8());
59 if (zone.isValid()) {
60 addTimeLabels(zone);
61 seenTimeZones += zoneStr;
62 }
63 }
64 }
65}
66
67void TimeLabelsZone::addTimeLabels(const QTimeZone &zone)
68{
69 auto area = new QScrollArea(this);
70 auto labels = new TimeLabels(zone, 24, this);
71 mTimeLabelsList.prepend(area);
72 area->setWidgetResizable(true);
73 area->setWidget(labels);
74 area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
75 area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
76 area->setBackgroundRole(QPalette::Window);
77 area->setFrameStyle(QFrame::NoFrame);
78 area->show();
79 mTimeLabelsLayout->insertWidget(0, area);
80
81 setupTimeLabel(area);
82}
83
84void TimeLabelsZone::setupTimeLabel(QScrollArea *area)
85{
86 if (mAgenda && mAgenda->verticalScrollBar()) {
87 // Scrolling the agenda will scroll the timelabel
89 // and vice-versa. ( this won't loop )
91
92 area->verticalScrollBar()->setValue(mAgenda->verticalScrollBar()->value());
93 }
94
95 auto timeLabels = static_cast<TimeLabels *>(area->widget());
96 timeLabels->setAgenda(mAgenda);
97
98 // timelabel's scroll is just a slave, this shouldn't be here
99 // if ( mParent ) {
100 // connect( area->verticalScrollBar(), SIGNAL(valueChanged(int)),
101 // mParent, SLOT(setContentsPos(int)) );
102 // }
103}
104
105int TimeLabelsZone::preferedTimeLabelsWidth() const
106{
107 if (mTimeLabelsList.isEmpty()) {
108 return 0;
109 } else {
110 return mTimeLabelsList.first()->widget()->sizeHint().width();
111 }
112}
113
114void TimeLabelsZone::updateAll()
115{
116 for (QScrollArea *area : std::as_const(mTimeLabelsList)) {
117 auto timeLabel = static_cast<TimeLabels *>(area->widget());
118 timeLabel->updateConfig();
119 }
120}
121
122QList<QScrollArea *> TimeLabelsZone::timeLabels() const
123{
124 return mTimeLabelsList;
125}
126
127void TimeLabelsZone::setAgendaView(AgendaView *agendaView)
128{
129 mParent = agendaView;
130 mAgenda = agendaView ? agendaView->agenda() : nullptr;
131
132 for (QScrollArea *timeLabel : std::as_const(mTimeLabelsList)) {
133 setupTimeLabel(timeLabel);
134 }
135}
136
137void TimeLabelsZone::updateTimeLabelsPosition()
138{
139 if (mAgenda) {
140 const auto lst = timeLabels();
141 for (QScrollArea *area : lst) {
142 auto label = static_cast<TimeLabels *>(area->widget());
143 const int adjustment = mAgenda->contentsY();
144 // y() is the offset to our parent (QScrollArea)
145 // and gets negative as we scroll
146 if (adjustment != -label->y()) {
147 area->verticalScrollBar()->setValue(adjustment);
148 }
149 }
150 }
151}
152
153PrefsPtr TimeLabelsZone::preferences() const
154{
155 return mPrefs;
156}
157
158void TimeLabelsZone::setPreferences(const PrefsPtr &prefs)
159{
160 if (prefs != mPrefs) {
161 mPrefs = prefs;
162 }
163}
164
165#include "moc_timelabelszone.cpp"
AgendaView is the agenda-like view that displays events in a single or multi-day view.
Definition agendaview.h:67
void createDayLabels(bool force)
Create labels for the selected dates.
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
QString label(StandardShortcut id)
const QList< QKeySequence > & preferences()
QCA_EXPORT void init()
QScrollBar * verticalScrollBar() const const
void setValue(int)
void valueChanged(int value)
void insertWidget(int index, QWidget *widget, int stretch, Qt::Alignment alignment)
void clear()
T & first()
bool isEmpty() const const
void prepend(parameter_type value)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QWidget * widget() const const
QString fromUtf8(QByteArrayView str)
ScrollBarAlwaysOff
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.