monthgraphicsitems.cpp 10.6 KB
Newer Older
1
/*
2
3
  SPDX-FileCopyrightText: 2008 Bruno Virlet <bruno.virlet@gmail.com>
  SPDX-FileCopyrightText: 2008 Thomas Thrainer <tom_t@gmx.at>
4

5
  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
*/

#include "monthgraphicsitems.h"
#include "eventview.h"
#include "helper.h"
#include "monthitem.h"
#include "monthscene.h"
#include "monthview.h"
#include "prefs.h"

#include <QGraphicsScene>
#include <QPainter>

using namespace EventViews;

ScrollIndicator::ScrollIndicator(ScrollIndicator::ArrowDirection dir)
    : mDirection(dir)
{
    setZValue(200);   // on top of everything
    hide();
}

QRectF ScrollIndicator::boundingRect() const
{
Laurent Montel's avatar
Laurent Montel committed
30
    return {-mWidth / 2, -mHeight / 2, mWidth, mHeight};
31
32
}

Laurent Montel's avatar
Laurent Montel committed
33
void ScrollIndicator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
34
{
Laurent Montel's avatar
Laurent Montel committed
35
36
    Q_UNUSED(option)
    Q_UNUSED(widget)
37
38
39
40
41

    painter->setRenderHint(QPainter::Antialiasing);

    QPolygon arrow(3);
    if (mDirection == ScrollIndicator::UpArrow) {
Laurent Montel's avatar
Laurent Montel committed
42
        arrow.setPoint(0, 0, -mHeight / 2);
43
        arrow.setPoint(1, mWidth / 2, mHeight / 2);
Laurent Montel's avatar
Laurent Montel committed
44
        arrow.setPoint(2, -mWidth / 2, mHeight / 2);
45
    } else if (mDirection == ScrollIndicator::DownArrow) {   // down
Laurent Montel's avatar
Laurent Montel committed
46
47
        arrow.setPoint(1, mWidth / 2, -mHeight / 2);
        arrow.setPoint(2, -mWidth / 2, -mHeight / 2);
48
49
        arrow.setPoint(0, 0, mHeight / 2);
    }
50
    QColor color(QPalette().color(QPalette::WindowText));
51
52
53
54
55
56
57
58
    color.setAlpha(155);
    painter->setBrush(color);
    painter->setPen(color);
    painter->drawPolygon(arrow);
}

//-------------------------------------------------------------
MonthCell::MonthCell(int id, const QDate &date, QGraphicsScene *scene)
Laurent Montel's avatar
Laurent Montel committed
59
60
61
    : mId(id)
    , mDate(date)
    , mScene(scene)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
    mUpArrow = new ScrollIndicator(ScrollIndicator::UpArrow);
    mDownArrow = new ScrollIndicator(ScrollIndicator::DownArrow);
    mScene->addItem(mUpArrow);
    mScene->addItem(mDownArrow);
}

MonthCell::~MonthCell()
{
    mScene->removeItem(mUpArrow);
    mScene->removeItem(mDownArrow);
    delete mUpArrow; // we've taken ownership, so this is safe
    delete mDownArrow;
}

bool MonthCell::hasEventBelow(int height)
{
    if (mHeightHash.isEmpty()) {
        return false;
    }

    for (int i = 0; i < height; ++i) {
Laurent Montel's avatar
Laurent Montel committed
84
        if (mHeightHash.value(i) != nullptr) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
            return true;
        }
    }

    return false;
}

int MonthCell::topMargin()
{
    return 18;
}

void MonthCell::addMonthItem(MonthItem *manager, int height)
{
    mHeightHash[ height ] = manager;
}

int MonthCell::firstFreeSpace()
{
Laurent Montel's avatar
Laurent Montel committed
104
    MonthItem *manager = nullptr;
105
106
107
    int i = 0;
    while (true) {
        manager = mHeightHash[ i ];
Laurent Montel's avatar
Laurent Montel committed
108
        if (manager == nullptr) {
109
110
111
112
113
114
115
116
            return i;
        }
        i++;
    }
}

//-------------------------------------------------------------
// MONTHGRAPHICSITEM
117
static const int ft = 1; // frame thickness
118
119

MonthGraphicsItem::MonthGraphicsItem(MonthItem *manager)
Laurent Montel's avatar
Laurent Montel committed
120
121
    : QGraphicsItem(nullptr)
    , mMonthItem(manager)
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
{
    manager->monthScene()->addItem(this);
    QTransform transform;
    transform = transform.translate(0.5, 0.5);
    setTransform(transform);
}

MonthGraphicsItem::~MonthGraphicsItem()
{
}

bool MonthGraphicsItem::isMoving() const
{
    return mMonthItem->isMoving();
}

bool MonthGraphicsItem::isEndItem() const
{
    return startDate().addDays(daySpan()) == mMonthItem->endDate();
}

bool MonthGraphicsItem::isBeginItem() const
{
    return startDate() == mMonthItem->startDate();
}

QPainterPath MonthGraphicsItem::shape() const
{
    // The returned shape must be a closed path,
    // otherwise MonthScene:itemAt(pos) can have
    // problems detecting the item
    return widgetPath(false);
}

// TODO: remove this method.
QPainterPath MonthGraphicsItem::widgetPath(bool border) const
{
    // If border is set we won't draw all the path. Items spanning on multiple
    // rows won't have borders on their boundaries.
    // If this is the mask, we draw it one pixel bigger
162
163
164
165
    const int x0 = (!border && !isBeginItem())? -1 : 0;
    const int y0 = 0;
    const int x1 = static_cast<int>(boundingRect().width());
    const int y1 = static_cast<int>(boundingRect().height());
166

167
168
    const int beginRound = 2;
    const int margin = 1;
169
170
171

    QPainterPath path(QPoint(x0 + beginRound, y0));
    if (isBeginItem()) {
172
173
174
        path.quadTo(QPoint(x0 + margin, y0), QPoint(x0 + margin, y0 + beginRound));
        path.lineTo(QPoint(x0 + margin, y1 - beginRound));
        path.quadTo(QPoint(x0 + margin, y1), QPoint(x0 + beginRound + margin, y1));
175
176
177
178
179
180
181
182
183
184
185
186
    } else {
        path.lineTo(x0, y0);
        if (!border) {
            path.lineTo(x0, y1);
        } else {
            path.moveTo(x0, y1);
        }
        path.lineTo(x0 + beginRound, y1);
    }

    if (isEndItem()) {
        path.lineTo(x1 - beginRound, y1);
187
188
189
        path.quadTo(QPoint(x1 - margin, y1), QPoint(x1 - margin, y1 - beginRound));
        path.lineTo(QPoint(x1 - margin, y0 + beginRound));
        path.quadTo(QPoint(x1 - margin, y0), QPoint(x1 - margin - beginRound, y0));
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    } else {
        path.lineTo(x1, y1);
        if (!border) {
            path.lineTo(x1, y0);
        } else {
            path.moveTo(x1, y0);
        }
    }

    // close path
    path.lineTo(x0 + beginRound, y0);

    return path;
}

QRectF MonthGraphicsItem::boundingRect() const
{
    // width - 2 because of the cell-dividing line with width == 1 at beginning and end
Laurent Montel's avatar
Laurent Montel committed
208
209
    return QRectF(0, 0, (daySpan() + 1) * mMonthItem->monthScene()->columnWidth() - 2,
                  mMonthItem->monthScene()->itemHeight());
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}

void MonthGraphicsItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (!mMonthItem->monthScene()->initialized()) {
        return;
    }

    MonthScene *scene = mMonthItem->monthScene();


    int textMargin = 7;

    QColor bgColor = mMonthItem->bgColor();
Laurent Montel's avatar
Laurent Montel committed
224
225
    bgColor = mMonthItem->selected()
              ? bgColor.lighter(EventView::BRIGHTNESS_FACTOR) : bgColor;
226
    QColor frameColor = mMonthItem->frameColor();
Laurent Montel's avatar
Laurent Montel committed
227
228
    frameColor = mMonthItem->selected()
                 ? frameColor.lighter(EventView::BRIGHTNESS_FACTOR) : frameColor;
229
230
231
232
233
234
235
    QColor textColor = EventViews::getTextColor(bgColor);

    // make moving or resizing items translucent
    if (mMonthItem->isMoving() || mMonthItem->isResizing()) {
        bgColor.setAlphaF(0.75f);
    }

236
237
238
    // draw the widget without border
    p->setRenderHint(QPainter::Antialiasing, false);
    p->setBrush(bgColor);
239
240
    p->setPen(Qt::NoPen);
    p->drawPath(widgetPath(false));
241
242
243
244
    
    p->setRenderHint(QPainter::Antialiasing, true);
    // draw the border without fill
    const QPen pen(frameColor, ft, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
245
246
247
248
    p->setPen(pen);
    p->setBrush(Qt::NoBrush);
    p->drawPath(widgetPath(true));

249
    // draw text
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    p->setPen(textColor);

    int alignFlag = Qt::AlignVCenter;
    if (isBeginItem()) {
        alignFlag |= Qt::AlignLeft;
    } else if (isEndItem()) {
        alignFlag |= Qt::AlignRight;
    } else {
        alignFlag |= Qt::AlignHCenter;
    }

    // !isBeginItem() is not always isEndItem()
    QString text = mMonthItem->text(!isBeginItem());
    p->setFont(mMonthItem->monthScene()->monthView()->preferences()->monthViewFont());

    // Every item should set its own LayoutDirection, or eliding fails miserably
    p->setLayoutDirection(text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight);

    QRect textRect = QRect(textMargin, 0,
                           static_cast<int>(boundingRect().width() - 2 * textMargin),
                           scene->itemHeight());

    if (mMonthItem->monthScene()->monthView()->preferences()->enableMonthItemIcons()) {
Laurent Montel's avatar
Laurent Montel committed
273
        const QVector<QPixmap> icons = mMonthItem->icons();
274
275
        int iconWidths = 0;

Laurent Montel's avatar
Laurent Montel committed
276
        for (const QPixmap &icon : icons) {
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
            iconWidths += icon.width();
        }

        if (!icons.isEmpty()) {
            // add some margin between the icons and the text
            iconWidths += textMargin / 2;
        }

        int textWidth = p->fontMetrics().size(0, text).width();
        if (textWidth + iconWidths > textRect.width()) {
            textWidth = textRect.width() - iconWidths;
            text = p->fontMetrics().elidedText(text, Qt::ElideRight, textWidth);
        }

        int curXPos = textRect.left();
        if (alignFlag & Qt::AlignRight) {
            curXPos += textRect.width() - textWidth - iconWidths;
        } else if (alignFlag & Qt::AlignHCenter) {
            curXPos += (textRect.width() - textWidth - iconWidths) / 2;
        }
        alignFlag &= ~(Qt::AlignRight | Qt::AlignCenter);
        alignFlag |= Qt::AlignLeft;

        // update the rect, where the text will be displayed
        textRect.setLeft(curXPos + iconWidths);

        // assume that all pixmaps have the same height
        int pixYPos = icons.isEmpty() ? 0 : (textRect.height() - icons.at(0).height()) / 2;
Laurent Montel's avatar
Laurent Montel committed
305
        for (const QPixmap &icon : qAsConst(icons)) {
306
307
308
309
310
311
312
313
314
315
316
            p->drawPixmap(curXPos, pixYPos, icon);
            curXPos += icon.width();
        }

        p->drawText(textRect, alignFlag | Qt::AlignVCenter, text);
    } else {
        text = p->fontMetrics().elidedText(text, Qt::ElideRight, textRect.width());
        p->drawText(textRect, alignFlag, text);
    }
}

Laurent Montel's avatar
Laurent Montel committed
317
void MonthGraphicsItem::setStartDate(QDate date)
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
{
    mStartDate = date;
}

QDate MonthGraphicsItem::endDate() const
{
    return startDate().addDays(daySpan());
}

QDate MonthGraphicsItem::startDate() const
{
    return mStartDate;
}

void MonthGraphicsItem::setDaySpan(int span)
{
    mDaySpan = span;
}

int MonthGraphicsItem::daySpan() const
{
    return mDaySpan;
}

void MonthGraphicsItem::updateGeometry()
{
Laurent Montel's avatar
Laurent Montel committed
344
    MonthCell *cell = mMonthItem->monthScene()->mMonthCellMap.value(startDate());
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

    // If the item is moving and this one is moved outside the view,
    // cell will be null
    if (mMonthItem->isMoving() && !cell) {
        hide();
        return;
    }

    Q_ASSERT(cell);

    prepareGeometryChange();

    int beginX = 1 + mMonthItem->monthScene()->cellHorizontalPos(cell);
    int beginY = 1 + cell->topMargin() + mMonthItem->monthScene()->cellVerticalPos(cell);

Laurent Montel's avatar
Laurent Montel committed
360
361
362
363
    beginY += mMonthItem->position()
              *mMonthItem->monthScene()->itemHeightIncludingSpacing()
              -mMonthItem->monthScene()->startHeight()
              *mMonthItem->monthScene()->itemHeightIncludingSpacing(); // scrolling
364
365
366

    setPos(beginX, beginY);

Laurent Montel's avatar
Laurent Montel committed
367
368
369
    if (mMonthItem->position() < mMonthItem->monthScene()->startHeight()
        || mMonthItem->position() - mMonthItem->monthScene()->startHeight()
        >= mMonthItem->monthScene()->maxRowCount()) {
370
371
372
373
374
375
376
377
378
379
380
        hide();
    } else {
        show();
        update();
    }
}

QString MonthGraphicsItem::getToolTip() const
{
    return mMonthItem->toolTipText(mStartDate);
}