timeline_layers_header.cpp 7.6 KB
Newer Older
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
/*
 *  Copyright (c) 2015 Dmitry Kazakov <dimula73@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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "timeline_layers_header.h"

#include "kis_debug.h"

#include <QPainter>
#include <QHelpEvent>
#include <QToolTip>

27
#include "timeline_frames_model.h"
28 29
#include "timeline_color_scheme.h"

30 31 32 33 34 35 36 37 38 39

struct TimelineLayersHeader::Private
{
    Private(TimelineLayersHeader *_q) : q(_q) {}

    TimelineLayersHeader *q;

    int numIcons(int logicalIndex) const;
    QRect iconRect(int logicalIndex, int iconIndex) const;
    int iconAt(int logicalIndex, const QPoint &pt);
40

41
    TimelineFramesModel::Property* getPropertyAt(TimelineFramesModel::PropertyList &props, int index);
42 43 44 45 46 47 48 49 50 51 52 53 54
};


TimelineLayersHeader::TimelineLayersHeader(QWidget *parent)
   : QHeaderView(Qt::Vertical, parent),
     m_d(new Private(this))
{
}

TimelineLayersHeader::~TimelineLayersHeader()
{
}

55 56
TimelineFramesModel::Property*
TimelineLayersHeader::Private::getPropertyAt(TimelineFramesModel::PropertyList &props, int index)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
    int logical = 0;
    for (int i = 0; i < props.size(); i++) {
        if (props[i].isMutable) {
            if (logical == index) {
                return &props[i];
            }

            logical++;
        }
    }

    return 0;
}

72 73 74 75
int TimelineLayersHeader::Private::numIcons(int logicalIndex) const
{
    int result = 0;

76
    QVariant value =  q->model()->headerData(logicalIndex, q->orientation(), TimelineFramesModel::TimelinePropertiesRole);
77
    if (value.isValid()) {
78
        TimelineFramesModel::PropertyList props = value.value<TimelineFramesModel::PropertyList>();
79

80
        Q_FOREACH (const TimelineFramesModel::Property &p, props) {
81 82 83 84
            if (p.isMutable) {
                result++;
            }
        }
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
    }

    return result;
}

QSize TimelineLayersHeader::sectionSizeFromContents(int logicalIndex) const
{
    QSize baseSize = QHeaderView::sectionSizeFromContents(logicalIndex);

    baseSize.setWidth(baseSize.width() + 6 + (2 + 16) * m_d->numIcons(logicalIndex));

    return baseSize;
}

QRect TimelineLayersHeader::Private::iconRect(int logicalIndex, int iconIndex) const
{
    QSize sectionSize(q->viewport()->width(), q->sectionSize(logicalIndex));

    const int iconWidth = 16;
    const int iconHeight = 16;

    const int y = (sectionSize.height() - iconHeight) / 2;
    const int x = sectionSize.width() -
        (numIcons(logicalIndex) - iconIndex) * (iconWidth + 2);


    return QRect(x, y, iconWidth, iconHeight);
}

void TimelineLayersHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    bool isLayerActive = model()->headerData(logicalIndex, orientation(), TimelineFramesModel::ActiveLayerRole).toBool();

    if (isLayerActive) {
        QColor lineColor = TimelineColorScheme::instance()->activeLayerBackground();
        const int lineWidth = 2;

        QPen oldPen = painter->pen();
        QBrush oldBrush(painter->brush());

        painter->setPen(QPen(lineColor, lineWidth));
        painter->setBrush(lineColor);

        const int x0 = rect.x();
        const int y0 = rect.y();
        const int x1 = rect.right();
        const int y1 = rect.bottom();

        QVector<QLine> lines;
        lines << QLine(x0, y0 + lineWidth / 2, x1, y0 + lineWidth / 2);
139
        lines << QLine(x0, y1 - lineWidth / 2, x1, y1 - lineWidth / 2);
140 141 142 143 144 145 146

        painter->drawLines(lines);

        painter->setBrush(oldBrush);
        painter->setPen(oldPen);
    }

147 148
    QVariant value =  model()->headerData(logicalIndex, orientation(), TimelineFramesModel::TimelinePropertiesRole);
    TimelineFramesModel::PropertyList props = value.value<TimelineFramesModel::PropertyList>();
149

150 151
    const int numIcons = m_d->numIcons(logicalIndex);
    for (int i = 0; i < numIcons; i++) {
152
        TimelineFramesModel::Property *p =
153
            m_d->getPropertyAt(props, i);
154

155 156
        const bool isActive = p->state.toBool();
        QIcon icon = isActive ? p->onIcon : p->offIcon;
157 158 159
        if (!isActive) {
            painter->setOpacity(0.35);
        }
160 161
        QRect rc = m_d->iconRect(logicalIndex, i).translated(rect.topLeft());
        icon.paint(painter, rc);
162
        painter->setOpacity(1.0);
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
    }
}

int TimelineLayersHeader::Private::iconAt(int logicalIndex, const QPoint &pt)
{
    QPoint sectionTopLeft(0,
                          q->sectionViewportPosition(logicalIndex));

    QPoint localPos = pt - sectionTopLeft;

    for (int i = 0; i < numIcons(logicalIndex); i++) {
        QRect rc = iconRect(logicalIndex, i);

        if (rc.contains(localPos)) {
            return i;
        }
    }

    return -1;
}

bool TimelineLayersHeader::viewportEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::ToolTip: {
        /**
         * Override tooltip if the cursor is over the properties icons.
         */
        QHelpEvent *he = static_cast<QHelpEvent*>(e);
        int logical = logicalIndexAt(he->pos());
        if (logical != -1) {

            const int iconIndex = m_d->iconAt(logical, he->pos());
            if (iconIndex != -1) {

198 199
                QVariant value =  model()->headerData(logical, orientation(), TimelineFramesModel::TimelinePropertiesRole);
                TimelineFramesModel::PropertyList props = value.value<TimelineFramesModel::PropertyList>();
200

201
                TimelineFramesModel::Property *p =
202 203
                    m_d->getPropertyAt(props, iconIndex);

204
                QString text = QString("%1 (%2)")
205 206
                    .arg(p->name)
                    .arg(p->state.toBool() ? "on" : "off");
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

                QToolTip::showText(he->globalPos(), text, this);
                return true;
            }
        }
        break; }
    default:
        break;
    }

    return QHeaderView::viewportEvent(e);
}

void TimelineLayersHeader::mousePressEvent(QMouseEvent *e)
{
    int logical = logicalIndexAt(e->pos());
    if (logical != -1) {

        const int iconIndex = m_d->iconAt(logical, e->pos());
        if (iconIndex != -1) {

228 229
            QVariant value =  model()->headerData(logical, orientation(), TimelineFramesModel::TimelinePropertiesRole);
            TimelineFramesModel::PropertyList props = value.value<TimelineFramesModel::PropertyList>();
230

231
            TimelineFramesModel::Property *p =
232 233 234 235
                m_d->getPropertyAt(props, iconIndex);

            bool currentState = p->state.toBool();
            p->state = !currentState;
236 237 238

            value.setValue(props);

239
            model()->setHeaderData(logical, orientation(), value, TimelineFramesModel::TimelinePropertiesRole);
240 241 242
            return;

        } else if (e->button() == Qt::RightButton) {
243
            model()->setHeaderData(logical, orientation(), true, TimelineFramesModel::ActiveLayerRole);
244 245
            emit sigRequestContextMenu(e->globalPos());
            return;
246
        } else if (e->button() == Qt::LeftButton) {
247
            model()->setHeaderData(logical, orientation(), true, TimelineFramesModel::ActiveLayerRole);
248 249 250 251 252 253 254
        }
    }

    QHeaderView::mousePressEvent(e);
}