File indexing completed on 2024-11-17 04:42:37

0001 /*
0002   SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno@virlet.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0005 */
0006 #include "timelabelszone.h"
0007 #include "agenda.h"
0008 #include "agendaview.h"
0009 #include "prefs.h"
0010 #include "timelabels.h"
0011 
0012 #include <QHBoxLayout>
0013 #include <QScrollArea>
0014 #include <QScrollBar>
0015 
0016 using namespace EventViews;
0017 
0018 TimeLabelsZone::TimeLabelsZone(QWidget *parent, const PrefsPtr &preferences, Agenda *agenda)
0019     : QWidget(parent)
0020     , mAgenda(agenda)
0021     , mPrefs(preferences)
0022     , mParent(qobject_cast<AgendaView *>(parent))
0023 {
0024     mTimeLabelsLayout = new QHBoxLayout(this);
0025     mTimeLabelsLayout->setContentsMargins(0, 0, 0, 0);
0026     mTimeLabelsLayout->setSpacing(0);
0027 
0028     init();
0029 }
0030 
0031 void TimeLabelsZone::reset()
0032 {
0033     for (QScrollArea *label : std::as_const(mTimeLabelsList)) {
0034         label->hide();
0035         label->deleteLater();
0036     }
0037     mTimeLabelsList.clear();
0038 
0039     init();
0040 
0041     // Update some related geometry from the agenda view
0042     updateAll();
0043     if (mParent) {
0044         mParent->updateTimeBarWidth();
0045         mParent->createDayLabels(true);
0046     }
0047 }
0048 
0049 void TimeLabelsZone::init()
0050 {
0051     QStringList seenTimeZones(QString::fromUtf8(mPrefs->timeZone().id()));
0052 
0053     addTimeLabels(mPrefs->timeZone());
0054 
0055     const auto lst = mPrefs->timeScaleTimezones();
0056     for (const QString &zoneStr : lst) {
0057         if (!seenTimeZones.contains(zoneStr)) {
0058             auto zone = QTimeZone(zoneStr.toUtf8());
0059             if (zone.isValid()) {
0060                 addTimeLabels(zone);
0061                 seenTimeZones += zoneStr;
0062             }
0063         }
0064     }
0065 }
0066 
0067 void TimeLabelsZone::addTimeLabels(const QTimeZone &zone)
0068 {
0069     auto area = new QScrollArea(this);
0070     auto labels = new TimeLabels(zone, 24, this);
0071     mTimeLabelsList.prepend(area);
0072     area->setWidgetResizable(true);
0073     area->setWidget(labels);
0074     area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0075     area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0076     area->setBackgroundRole(QPalette::Window);
0077     area->setFrameStyle(QFrame::NoFrame);
0078     area->show();
0079     mTimeLabelsLayout->insertWidget(0, area);
0080 
0081     setupTimeLabel(area);
0082 }
0083 
0084 void TimeLabelsZone::setupTimeLabel(QScrollArea *area)
0085 {
0086     if (mAgenda && mAgenda->verticalScrollBar()) {
0087         // Scrolling the agenda will scroll the timelabel
0088         connect(mAgenda->verticalScrollBar(), &QAbstractSlider::valueChanged, area->verticalScrollBar(), &QAbstractSlider::setValue);
0089         // and vice-versa. ( this won't loop )
0090         connect(area->verticalScrollBar(), &QAbstractSlider::valueChanged, mAgenda->verticalScrollBar(), &QAbstractSlider::setValue);
0091 
0092         area->verticalScrollBar()->setValue(mAgenda->verticalScrollBar()->value());
0093     }
0094 
0095     auto timeLabels = static_cast<TimeLabels *>(area->widget());
0096     timeLabels->setAgenda(mAgenda);
0097 
0098     // timelabel's scroll is just a slave, this shouldn't be here
0099     // if ( mParent ) {
0100     //  connect( area->verticalScrollBar(), SIGNAL(valueChanged(int)),
0101     //           mParent, SLOT(setContentsPos(int)) );
0102     // }
0103 }
0104 
0105 int TimeLabelsZone::preferedTimeLabelsWidth() const
0106 {
0107     if (mTimeLabelsList.isEmpty()) {
0108         return 0;
0109     } else {
0110         return mTimeLabelsList.first()->widget()->sizeHint().width();
0111     }
0112 }
0113 
0114 void TimeLabelsZone::updateAll()
0115 {
0116     for (QScrollArea *area : std::as_const(mTimeLabelsList)) {
0117         auto timeLabel = static_cast<TimeLabels *>(area->widget());
0118         timeLabel->updateConfig();
0119     }
0120 }
0121 
0122 QList<QScrollArea *> TimeLabelsZone::timeLabels() const
0123 {
0124     return mTimeLabelsList;
0125 }
0126 
0127 void TimeLabelsZone::setAgendaView(AgendaView *agendaView)
0128 {
0129     mParent = agendaView;
0130     mAgenda = agendaView ? agendaView->agenda() : nullptr;
0131 
0132     for (QScrollArea *timeLabel : std::as_const(mTimeLabelsList)) {
0133         setupTimeLabel(timeLabel);
0134     }
0135 }
0136 
0137 void TimeLabelsZone::updateTimeLabelsPosition()
0138 {
0139     if (mAgenda) {
0140         const auto lst = timeLabels();
0141         for (QScrollArea *area : lst) {
0142             auto label = static_cast<TimeLabels *>(area->widget());
0143             const int adjustment = mAgenda->contentsY();
0144             // y() is the offset to our parent (QScrollArea)
0145             // and gets negative as we scroll
0146             if (adjustment != -label->y()) {
0147                 area->verticalScrollBar()->setValue(adjustment);
0148             }
0149         }
0150     }
0151 }
0152 
0153 PrefsPtr TimeLabelsZone::preferences() const
0154 {
0155     return mPrefs;
0156 }
0157 
0158 void TimeLabelsZone::setPreferences(const PrefsPtr &prefs)
0159 {
0160     if (prefs != mPrefs) {
0161         mPrefs = prefs;
0162     }
0163 }
0164 
0165 #include "moc_timelabelszone.cpp"