File: BreadCrumbBar.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (467 lines) | stat: -rw-r--r-- 12,221 bytes parent folder | download | duplicates (3)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
305
306
307
308
309
310
311
312
313
314
315
316
317
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "BreadCrumbBar.h"

#include <QApplication>
#include <QFont>
#include <QHBoxLayout>
#include <QLinearGradient>
#include <QMenu>
#include <QPaintEvent>
#include <QPainter>
#include <QStyleOptionFocusRect>
#include <QStyle>

#define BAR_RADIUS 6
#define BAR_H_MARGIN 7
#define BAR_V_MARGIN 2

/// BcLabel

BcLabel::BcLabel(quint32 labelId, QWidget * parent)
  : QLabel(parent)
  , m_labId(labelId)
  , m_last(true)
  , m_hover(false)
{
    // shrink font
    QFont font;
    font.setPointSize(font.pointSize() - 1);
    setFont(font);
}

void BcLabel::setLast(bool last)
{
    m_last = last;
    QPalette pal;
    pal.setBrush(QPalette::Text, m_last ? Qt::darkRed : Qt::darkGray);
    setPalette(pal);
    setCursor(m_last ? Qt::ArrowCursor : Qt::PointingHandCursor);
    setFocusPolicy(m_last ? Qt::NoFocus : Qt::StrongFocus);
    update();
}

bool BcLabel::last() const
{
    return m_last;
}

void BcLabel::enterEvent(QEvent * event)
{
    QLabel::enterEvent(event);
    m_hover = true;
    update();
}

void BcLabel::leaveEvent(QEvent * event)
{
    QLabel::leaveEvent(event);
    m_hover = false;
    update();
}

void BcLabel::keyPressEvent(QKeyEvent * event)
{
    QLabel::keyPressEvent(event);
    if (!m_last && (event->key() == Qt::Key_Space || event->key() == Qt::Key_Enter)) {
        event->accept();
        emit labelClicked(m_labId);
    }
}

void BcLabel::mousePressEvent(QMouseEvent * event)
{
    QLabel::mousePressEvent(event);
    if (!m_last && event->button() == Qt::LeftButton) {
        event->accept();
        emit labelClicked(m_labId);
    }
}

void BcLabel::paintEvent(QPaintEvent * event)
{
    // draw underlining if clickable
    if (!m_last && m_hover) {
        QPainter p(this);
        p.setPen(QPen(QPalette().highlight().color(), 1));
        p.drawLine(0, height() - 2, width(), height() - 2);
    }

    // unbreak painting
    QLabel::paintEvent(event);

    // focus handling
    if (hasFocus()) {
        QPainter p(this);
        QStyleOptionFocusRect opt;
        opt.initFrom(this);
        opt.backgroundColor = Qt::white;
        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, &p, this);
    }
}


/// BcExpander

BcExpander::BcExpander(quint32 expanderId, QWidget * parent)
  : QWidget(parent)
  , m_exId(expanderId)
  , m_count(1)
{
    setFixedSize(11, 11);
}

void BcExpander::setCount(int count)
{
    m_count = count;
    setCursor(count > 1 ? Qt::PointingHandCursor : Qt::ArrowCursor);
    setFocusPolicy(count > 1 ? Qt::StrongFocus : Qt::NoFocus);
    update();
}

int BcExpander::count() const
{
    return m_count;
}

void BcExpander::mousePressEvent(QMouseEvent * event)
{
    if (m_count > 1 && event->button() == Qt::LeftButton)
        emit expanderClicked(m_exId);
}

void BcExpander::paintEvent(QPaintEvent * /*event*/)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(QPen(Qt::lightGray, 2));
    if (m_count > 1) {
        if (QApplication::isRightToLeft()) {
            p.drawLine(5, 1, 1, 5);
            p.drawLine(1, 5, 5, 9);
            p.drawLine(5, 9, 9, 5);
        } else {
            p.drawLine(5, 1, 9, 5);
            p.drawLine(9, 5, 5, 9);
            p.drawLine(5, 9, 1, 5);
        }
    } else {
        if (QApplication::isRightToLeft()) {
            p.drawLine(7, 1, 3, 5);
            p.drawLine(3, 5, 7, 9);
        } else {
            p.drawLine(3, 1, 7, 5);
            p.drawLine(7, 5, 3, 9);
        }
    }

    // focus handling
    if (hasFocus()) {
        QStyleOptionFocusRect opt;
        opt.initFrom(this);
        opt.backgroundColor = Qt::white;
        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, &p, this);
    }
}


/// Internal Node (BreadCrumbBar)

struct InternalNode {
    quint32 id;
    QString text;
    InternalNode * parent;
    QList<InternalNode *> children;

    BcLabel * label;
    BcExpander * expander;

    InternalNode(quint32 id, const QString & text)
      : id(id)
      , text(text)
      , parent(0)
      , label(0)
      , expander(0)
    {
    }
    ~InternalNode()
    {
        if (parent) {
            parent->children.removeAll(this);
            parent->clearWidgetry();
        }
        qDeleteAll(children);
        delete expander;
        delete label;
    }
    InternalNode * findNode(quint32 searchId) {
        if (id == searchId)
            return this;
        foreach (InternalNode * child, children)
            if (InternalNode * cNode = child->findNode(searchId))
                return cNode;
        return 0;
    }
    void clearWidgetry() {
        delete expander;
        expander = 0;
        delete label;
        label = 0;
        foreach (InternalNode * child, children)
            child->clearWidgetry();
    }
};


/// BreadCrumbBar

BreadCrumbBar::BreadCrumbBar(QWidget * parent)
  : QWidget(parent)
  , m_root(0)
  , m_clickableLeaves(true)
  , m_drawBackground(false)
  , m_backgroundOffset(0)
{
    // init defaults
    processLayout();
}

quint32 BreadCrumbBar::addNode(quint32 id, const QString & text, quint32 parentId)
{
    // sanity check: duplicated id
    if (m_root && m_root->findNode(id)) {
        qWarning("BreadCrumbBar::addNode: already present a node with id 0x%x. skipping", id);
        return InvalidNode;
    }

    // find out where to insert the node
    InternalNode * parentNode = (parentId == InvalidNode) ? 0 : (m_root ? m_root->findNode(parentId) : 0);
    if (m_root && !parentNode) {
        qWarning("BreadCrumbBar::addNode: can't add more than 1 node to the root");
        return InvalidNode;
    }

    // create the node
    InternalNode * node = new InternalNode(id, text);
    if (parentNode) {
        parentNode->children.append(node);
        node->parent = parentNode;
    }
    if (!m_root)
        m_root = node;
    processLayout();
    return id;
}

void BreadCrumbBar::deleteNode(quint32 id)
{
    if (!m_root)
        return;
    InternalNode * node = m_root->findNode(id);
    if (!node)
        return;
    delete node;
    if (node == m_root)
        m_root = 0;
    processLayout();
}

void BreadCrumbBar::clearNodes()
{
    delete m_root;
    m_root = 0;
    processLayout();
}

void BreadCrumbBar::setClickableLeaves(bool clickable)
{
    if (m_clickableLeaves != clickable) {
        m_clickableLeaves = clickable;
        processLayout();
    }
}

bool BreadCrumbBar::clickableLeaves() const
{
    return m_clickableLeaves;
}

void BreadCrumbBar::setDrawBackground(bool enable)
{
    if (m_drawBackground != enable) {
        m_drawBackground = enable;
        update();
    }
}

bool BreadCrumbBar::drawBackground() const
{
    return m_drawBackground;
}

void BreadCrumbBar::setBackgroundOffset(int side)
{
    if (m_backgroundOffset != side) {
        m_backgroundOffset = side;
        update();
    }
}

int BreadCrumbBar::backgroundOffset() const
{
    return m_backgroundOffset;
}

void BreadCrumbBar::paintEvent(QPaintEvent * /*event*/)
{
    // skip drawing if not requested
    if (!m_drawBackground)
        return;

    // find out the painting boundaries
    QRectF boundaries = rect();
    boundaries.adjust(0.5, 0.5, -0.5, -0.5);
#if 0
    // find children boundaries
    if (layout()) {
        boundaries = QRectF();
        const QLayout * lay = layout();
        const int children = lay->count();
        for (int i = 0; i < children; ++i) {
            QLayoutItem * child = lay->itemAt(i);
            if (child->widget())
                boundaries = boundaries.isNull() ? child->geometry() : boundaries.united(child->geometry());
        }
        boundaries.adjust(-BAR_H_MARGIN + 0.5, -BAR_V_MARGIN + 0.5, BAR_H_MARGIN - 0.5, BAR_V_MARGIN - 0.5);
    }
#endif

    // adapt a bit the boundaries to hide a round side
    if (m_backgroundOffset) {
        bool adjustLeft = false;
        adjustLeft = m_backgroundOffset > 0;
        if (QApplication::isRightToLeft())
            adjustLeft = !adjustLeft;
        if (adjustLeft)
            boundaries.adjust(0, 0, BAR_RADIUS, 0);
        else
            boundaries.adjust(-BAR_RADIUS, 0, 0, 0);
    }

    // paint a rounded rect
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(QPen(Qt::darkGray, 1));
    QLinearGradient lg(0, 0, 0, height());
    lg.setColorAt(0.0, QColor(255, 255, 255));
    lg.setColorAt(1.0, QColor(200, 200, 200));
    p.setBrush(lg);
    p.drawRoundedRect(boundaries, BAR_RADIUS, BAR_RADIUS, Qt::AbsoluteSize);
}

void BreadCrumbBar::processLayout()
{
    // recreate the layout
    delete layout();
    QHBoxLayout * hLay = new QHBoxLayout(this);
    hLay->setContentsMargins(BAR_H_MARGIN, BAR_V_MARGIN, BAR_H_MARGIN, BAR_V_MARGIN);
    hLay->setSpacing(6);
    setLayout(hLay);

    // hide if empty
    if (!m_root) {
        hide();
        return;
    }
    show();

    // build up the layout (and widgets too, if needed)
    InternalNode * node = m_root;
    while (node) {
        // create label if missing
        if (!node->label) {
            node->label = new BcLabel(node->id, this);
            node->label->setText(node->text);
            connect(node->label, SIGNAL(labelClicked(quint32)), this, SLOT(slotLabelClicked(quint32)));
        }
        node->label->setLast(node->children.isEmpty() && !m_clickableLeaves);
        hLay->addWidget(node->label);

        // create/destroy expander when needed
        if (node->children.isEmpty()) {
            if (node->expander) {
                delete node->expander;
                node->expander = 0;
            }
            break;
        }
        if (!node->expander) {
            node->expander = new BcExpander(node->id, this);
            connect(node->expander, SIGNAL(expanderClicked(quint32)), this, SLOT(slotExpanderClicked(quint32)));
        }
        node->expander->setCount(node->children.size());
        hLay->addWidget(node->expander);

        // continue the chain
        node = node->children.first();
    }

    // add a stretch to the end
    hLay->addStretch(10);

    // repaint all
    update();
}

void BreadCrumbBar::slotLabelClicked(quint32 id)
{
    emit nodeClicked(id);
}

void BreadCrumbBar::slotExpanderClicked(quint32 parentId)
{
    InternalNode * pNode = m_root ? m_root->findNode(parentId) : 0;
    if (!pNode)
        return;

    // create menu
    QMenu menu;
    int listIndex = 0;
    foreach (InternalNode * child, pNode->children) {
        QAction * action = menu.addAction(child->text);
        if (!listIndex) {
            QFont boldFont;
            boldFont.setBold(true);
            action->setFont(boldFont);
        }
        action->setProperty("listIndex", listIndex++);
    }

    // show menu
    BcExpander * expander = static_cast<BcExpander *>(sender());
    QAction * action = menu.exec(expander->mapToGlobal(QPoint(0, expander->height())));
    if (!action)
        return;

    // change the branch
    listIndex = action->property("listIndex").toInt();
    if (listIndex > 0 && listIndex < pNode->children.size()) {
        pNode->children.first()->clearWidgetry();
        InternalNode * node = pNode->children.takeAt(listIndex);
        pNode->children.prepend(node);
        processLayout();
        emit nodeClicked(node->id);
    }
}