PageRenderTime 32ms CodeModel.GetById 9ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/rotatedheader.cpp

https://code.google.com/p/dwarftherapist/
C++ | 217 lines | 150 code | 23 blank | 44 comment | 30 complexity | 4b5ade75c6d595db9607524236be0d59 MD5 | raw file
  1/*
  2Dwarf Therapist
  3Copyright (c) 2009 Trey Stout (chmod)
  4
  5Permission is hereby granted, free of charge, to any person obtaining a copy
  6of this software and associated documentation files (the "Software"), to deal
  7in the Software without restriction, including without limitation the rights
  8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the Software is
 10furnished to do so, subject to the following conditions:
 11
 12The above copyright notice and this permission notice shall be included in
 13all copies or substantial portions of the Software.
 14
 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21THE SOFTWARE.
 22*/
 23#include "rotatedheader.h"
 24#include "dwarfmodel.h"
 25#include "dwarfmodelproxy.h"
 26#include "dwarftherapist.h"
 27#include "defines.h"
 28
 29RotatedHeader::RotatedHeader(Qt::Orientation orientation, QWidget *parent)
 30    : QHeaderView(orientation, parent)
 31    , m_hovered_column(-1)
 32{
 33    setClickable(true);
 34    setSortIndicatorShown(true);
 35    setMouseTracking(true);
 36
 37    read_settings();
 38    connect(DT, SIGNAL(settings_changed()), this, SLOT(read_settings()));
 39}
 40
 41void RotatedHeader::column_hover(int col) {
 42    updateSection(m_hovered_column);
 43    m_hovered_column = col;
 44    updateSection(col);
 45}
 46
 47void RotatedHeader::read_settings() {
 48    QSettings *s = DT->user_settings();
 49    int cell_size = s->value("options/grid/cell_size", DEFAULT_CELL_SIZE).toInt();
 50    for(int i=1; i < count(); ++i) {
 51        if (!m_spacer_indexes.contains(i)) {
 52            resizeSection(i, cell_size);
 53        }
 54    }
 55    m_shade_column_headers = s->value("options/grid/shade_column_headers", true).toBool();
 56}
 57
 58void RotatedHeader::paintSection(QPainter *p, const QRect &rect, int idx) const {
 59    if (!rect.isValid() || idx == 0)
 60        return QHeaderView::paintSection(p, rect, idx);
 61
 62    QColor bg = model()->headerData(idx, Qt::Horizontal,
 63                                    Qt::BackgroundColorRole).value<QColor>();
 64    if (m_spacer_indexes.contains(idx)) {
 65        p->save();
 66        p->fillRect(rect, QBrush(bg));
 67        p->restore();
 68        return;
 69    }
 70
 71    QStyleOptionHeader opt;
 72    opt.rect = rect;
 73    opt.orientation = Qt::Horizontal;
 74    opt.section = idx;
 75    opt.sortIndicator = QStyleOptionHeader::None;
 76
 77    QStyle::State state = QStyle::State_None;
 78    if (isEnabled())
 79        state |= QStyle::State_Enabled;
 80    if (window()->isActiveWindow())
 81        state |= QStyle::State_Active;
 82    if (rect.contains(m_p))
 83        state |= QStyle::State_MouseOver;
 84    if (sortIndicatorSection() == idx) {
 85        //state |= QStyle::State_Sunken;
 86        if (sortIndicatorOrder() == Qt::AscendingOrder) {
 87            opt.sortIndicator = QStyleOptionHeader::SortDown;
 88        } else {
 89            opt.sortIndicator = QStyleOptionHeader::SortUp;
 90        }
 91    }
 92    if (m_hovered_column == idx) {
 93        state |= QStyle::State_MouseOver;
 94    }
 95
 96    opt.state = state;
 97    style()->drawControl(QStyle::CE_HeaderSection, &opt, p);
 98
 99    QBrush brush = QBrush(bg);
100    if (m_shade_column_headers) {
101        QLinearGradient g(rect.topLeft(), rect.bottomLeft());
102        g.setColorAt(0.25, QColor(255, 255, 255, 10));
103        g.setColorAt(1.0, bg);
104        brush = QBrush(g);
105    }
106    if (idx > 0)
107        p->fillRect(rect.adjusted(1,8,-1,-2), brush);
108
109    if (sortIndicatorSection() == idx) {
110        opt.rect = QRect(opt.rect.x() + opt.rect.width()/2 - 5, opt.rect.y(), 10, 8);
111        style()->drawPrimitive(QStyle::PE_IndicatorHeaderArrow, &opt, p);
112    }
113
114    /* Draw a border around header if column has guides applied
115    DwarfModelProxy *prox = static_cast<DwarfModelProxy*>(model());
116    DwarfModel *dm = prox->get_dwarf_model();
117    int col = dm->selected_col();
118    if (dm->selected_col() == idx) {
119        p->save();
120        p->setPen(Qt::red);
121        p->setBrush(Qt::NoBrush);
122        p->drawRect(rect);
123        p->restore();
124    }
125    */
126
127    QString data = this->model()->headerData(idx, Qt::Horizontal).toString();
128    p->save();
129    p->setPen(Qt::black);
130    p->setRenderHint(QPainter::Antialiasing);
131    p->translate(rect.x(), rect.y());
132    p->rotate(90);
133    p->setFont(QFont("Verdana", 8));
134    p->drawText(14, -4, data);
135    p->restore();
136}
137
138void RotatedHeader::resizeSection(int logicalIndex, int size) {
139    QHeaderView::resizeSection(logicalIndex, size);
140}
141
142void RotatedHeader::set_index_as_spacer(int idx) {
143    m_spacer_indexes << idx;
144}
145
146void RotatedHeader::clear_spacers() {
147    m_spacer_indexes.clear();
148}
149
150QSize RotatedHeader::sizeHint() const {
151    return QSize(32, 150);
152}
153
154void RotatedHeader::mouseMoveEvent(QMouseEvent *e) {
155    m_p = e->pos();
156    QHeaderView::mouseMoveEvent(e);
157}
158
159void RotatedHeader::mousePressEvent(QMouseEvent *e) {
160    m_p = e->pos();
161    int idx = logicalIndexAt(e->pos());
162    if (idx > 0 && idx < count() && e->button() == Qt::RightButton) {
163        emit section_right_clicked(idx);
164    }
165    QHeaderView::mousePressEvent(e);
166}
167
168void RotatedHeader::leaveEvent(QEvent *e) {
169    m_p = QPoint(-1, -1);
170    QHeaderView::leaveEvent(e);
171}
172
173void RotatedHeader::contextMenuEvent(QContextMenuEvent *evt) {
174    int idx = logicalIndexAt(evt->pos());
175    if (idx == 0) { //name header
176        QMenu *m = new QMenu(this);
177        QAction *a = m->addAction(tr("Sort Alphabetically Ascending"), this, SLOT(sort_action()));
178        a->setData(DwarfModelProxy::DSR_NAME_ASC);
179        a = m->addAction(tr("Sort Alphabetically Descending"), this, SLOT(sort_action()));
180        a->setData(DwarfModelProxy::DSR_NAME_DESC);
181        a = m->addAction(tr("Sort by ID Ascending"), this, SLOT(sort_action()));
182        a->setData(DwarfModelProxy::DSR_ID_ASC);
183        a = m->addAction(tr("Sort by ID Descending"), this, SLOT(sort_action()));
184        a->setData(DwarfModelProxy::DSR_ID_DESC);
185        a = m->addAction(tr("Sort in Game Order"), this, SLOT(sort_action()));
186        a->setData(DwarfModelProxy::DSR_GAME_ORDER);
187        m->exec(viewport()->mapToGlobal(evt->pos()));
188    } else {
189        /* Don't do this yet
190        // find out what set this is...
191        QString set_name = model()->headerData(idx, Qt::Horizontal, Qt::UserRole).toString();
192        QMenu *m = new QMenu(this);
193        QAction *a = m->addAction(tr("Toggle Set %1").arg(set_name),
194                                  this, SLOT(toggle_set_action()));
195        a->setData(set_name);
196        m->exec(viewport()->mapToGlobal(evt->pos()));
197        */
198    }
199}
200
201void RotatedHeader::sort_action() {
202    QAction *sender = qobject_cast<QAction*>(QObject::sender());
203    DwarfModelProxy::DWARF_SORT_ROLE role = static_cast<DwarfModelProxy::DWARF_SORT_ROLE>(sender->data().toInt());
204    emit sort(0, role);
205}
206
207void RotatedHeader::toggle_set_action() {
208    QAction *sender = qobject_cast<QAction*>(QObject::sender());
209    QString set_name = sender->data().toString();
210    for(int i = 1; i < count(); ++i) {
211        QString col_set_name = model()->headerData(i, Qt::Horizontal,
212                                                   Qt::UserRole).toString();
213        if (col_set_name == set_name) {
214            hideSection(i);
215        }
216    }
217}