dtkWidgetsWorkspaceStackBar.cpp 5.43 KB
Newer Older
1
#include "dtkWidgetsWorkspaceStackBar.h"
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
2
#include "dtkWidgetsWorkspace.h"
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
class dtkWidgetsWorkspaceStackBarOverview : public QLabel
{
public:
    dtkWidgetsWorkspaceStackBarOverview(QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *) override;
};

dtkWidgetsWorkspaceStackBarOverview::dtkWidgetsWorkspaceStackBarOverview(QWidget *parent): QLabel(parent)
{
}

void dtkWidgetsWorkspaceStackBarOverview::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);

    QFontMetrics metrics(qApp->font());

    const int offset = 30;
    const int margin = 20;

    QString text = this->objectName();
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(0, 0, 0, 128));
    painter.drawRect(0, this->size().height() - offset - margin, this->size().width(), margin * 7/4);
    painter.setPen(Qt::white);
    painter.drawText(this->size().width()/2 - metrics.width(text)/2, this->size().height() - offset, text);
}

40
class dtkWidgetsWorkspaceStackBarItem : public QFrame
41 42 43 44 45
{
    Q_OBJECT
    Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)

public:
46 47
     dtkWidgetsWorkspaceStackBarItem(int index, QWidget *parent = nullptr);
    ~dtkWidgetsWorkspaceStackBarItem(void);
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

signals:
    void clicked(int);

signals:
    void activeChanged(void);

public:
    bool active(void) {
        return this->on;
    }

    void setActive(bool active) {
        this->on = active;

        emit activeChanged();
    }

public:
    void setSource(QWidget *);

protected:
    void mousePressEvent(QMouseEvent *);

private:
    QWidget *source = nullptr;

private:
76
    dtkWidgetsWorkspaceStackBarOverview *overview;
77 78 79 80 81 82 83 84

private:
    int index = -1;

private:
    bool on = false;
};

85
dtkWidgetsWorkspaceStackBarItem::dtkWidgetsWorkspaceStackBarItem(int index, QWidget *parent) : QFrame(parent)
86 87 88
{
    this->index = index;

89
    this->overview = new dtkWidgetsWorkspaceStackBarOverview(this);
90 91 92 93
    this->overview->setObjectName("overview");
    this->overview->setAlignment(Qt::AlignCenter);

    QVBoxLayout *layout = new QVBoxLayout(this);
94
    layout->setContentsMargins(0, 0, 0, 0);
95 96 97 98 99
    layout->addStretch();
    layout->addWidget(this->overview);
    layout->addStretch();
}

100
dtkWidgetsWorkspaceStackBarItem::~dtkWidgetsWorkspaceStackBarItem(void)
101 102 103 104
{

}

105
void dtkWidgetsWorkspaceStackBarItem::setSource(QWidget *source)
106 107 108
{
    this->source = source;

109
    this->overview->setObjectName(this->source->objectName());
110 111

    QTimer *timer = new QTimer(this);
112
    timer->setInterval(1000);
113 114 115
    timer->setSingleShot(false);

    connect(timer, &QTimer::timeout, [=] (void) {
116 117
        if (this->visibleRegion().isNull())
            return;
118 119 120 121 122 123 124 125
        QPixmap pixmap(this->source->size());
        this->source->render(&pixmap);
        this->overview->setPixmap(pixmap.scaledToWidth(280));
    });

    timer->start();
}

126
void dtkWidgetsWorkspaceStackBarItem::mousePressEvent(QMouseEvent *event)
127 128 129 130 131 132 133 134 135 136
{
    Q_UNUSED(event);

    emit clicked(this->index);
}

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

137
class dtkWidgetsWorkspaceStackBarPrivate
138 139 140 141 142
{
public:
    QStackedWidget *stack = nullptr;

public:
143
    QList<dtkWidgetsWorkspaceStackBarItem *> items;
144 145 146 147 148 149 150 151 152

public:
    QVBoxLayout *layout;
};

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

153
dtkWidgetsWorkspaceStackBar::dtkWidgetsWorkspaceStackBar(QWidget *parent) : QFrame(parent)
154
{
155
    d = new dtkWidgetsWorkspaceStackBarPrivate;
156 157 158

    d->layout = new QVBoxLayout(this);
    d->layout->setContentsMargins(0, 0, 1, 0);
159
    d->layout->setSpacing(1);
160 161 162

}

163
dtkWidgetsWorkspaceStackBar::~dtkWidgetsWorkspaceStackBar(void)
164 165 166 167
{
    delete d;
}

168
QSize dtkWidgetsWorkspaceStackBar::sizeHint(void) const
169 170 171 172
{
    return QSize(300, 100);
}

173
void dtkWidgetsWorkspaceStackBar::setStack(QStackedWidget *stack)
174 175 176 177 178 179 180 181
{
    d->stack = stack;

    if(!d->stack->count())
        qDebug() << "Please setup stack before computing the bar.";

    for (int i = 0; i < d->stack->count(); i++) {

182
        dtkWidgetsWorkspaceStackBarItem *item = new dtkWidgetsWorkspaceStackBarItem(i, this);
183 184
        item->setSource(d->stack->widget(i));

185 186 187 188 189 190 191
        connect(item, &dtkWidgetsWorkspaceStackBarItem::clicked, [=] (int i) {
            dtkWidgetsWorkspace *previous = dynamic_cast<dtkWidgetsWorkspace*>(d->stack->currentWidget());
            previous->leave();
            d->stack->setCurrentIndex(i);
            dtkWidgetsWorkspace *current = dynamic_cast<dtkWidgetsWorkspace*>(d->stack->currentWidget());
            current->enter();
        });
192
        connect(item, SIGNAL(clicked(int)), d->stack, SLOT(setCurrentIndex(int)));
193
        connect(item, &dtkWidgetsWorkspaceStackBarItem::clicked, [=] (int) {
194

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
195
            for(dtkWidgetsWorkspaceStackBarItem *it : d->items) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
                this->style()->unpolish(it);
                it->setActive(it == item);
                this->style()->polish(it);
             }
        });

        d->layout->addWidget(item);
        d->items << item;
    }

    d->items.first()->setActive(true);
}

// ///////////////////////////////////////////////////////////////////

211
#include "dtkWidgetsWorkspaceStackBar.moc"
212 213

//
214
// dtkWidgetsWorkspaceStackBar.cpp ends here