aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreeitemdelegate.cpp
blob: ef691d21b3802d3adcc61fa6dd5d29be64369d64 (plain) (blame)
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
#include "dbtreeitemdelegate.h"
#include "dbtreeitem.h"
#include "dbtreemodel.h"
#include "common/utils_sql.h"
#include "uiconfig.h"
#include "dbtree.h"
#include "dbtreeview.h"
#include <QPainter>
#include <QDebug>

DbTreeItemDelegate::DbTreeItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QSize DbTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = QStyledItemDelegate::sizeHint(option, index);

    QFont f = CFG_UI.Fonts.DbTree.get();
    QFontMetrics fm(f);
    size.setHeight(qMax(18, fm.height()));
    return size;
}

void DbTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    const DbTreeModel* model = dynamic_cast<const DbTreeModel*>(index.model());
    DbTreeItem* item = dynamic_cast<DbTreeItem*>(model->itemFromIndex(index));

    opt.font = CFG_UI.Fonts.DbTree.get();
    opt.fontMetrics = QFontMetrics(opt.font);

    QModelIndex currIndex = DBTREE->getView()->selectionModel()->currentIndex();
    if (currIndex.isValid() && item->index() == currIndex)
        opt.state |= QStyle::State_HasFocus;

    QStyledItemDelegate::paint(painter, opt, index);

    if (!CFG_UI.General.ShowDbTreeLabels.get())
        return;

    switch (item->getType())
    {
        case DbTreeItem::Type::DIR:
            break;
        case DbTreeItem::Type::DB:
            paintDb(painter, opt, index, item);
            break;
        case DbTreeItem::Type::TABLES:
        case DbTreeItem::Type::INDEXES:
        case DbTreeItem::Type::TRIGGERS:
        case DbTreeItem::Type::VIEWS:
        case DbTreeItem::Type::COLUMNS:
            paintChildCount(painter, opt, index, item);
            break;
        case DbTreeItem::Type::TABLE:
            paintTableLabel(painter, opt, index, item);
            break;
        case DbTreeItem::Type::VIRTUAL_TABLE:
            paintVirtualTableLabel(painter, opt, index, item);
            break;
        case DbTreeItem::Type::INDEX:
            paintSystemIndexLabel(painter, opt, index, item);
            break;
        case DbTreeItem::Type::TRIGGER:
        case DbTreeItem::Type::VIEW:
        case DbTreeItem::Type::COLUMN:
        case DbTreeItem::Type::ITEM_PROTOTYPE:
            break;
    }
}

void DbTreeItemDelegate::paintDb(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, DbTreeItem *item) const
{
    static const QString versionStringTemplate = QStringLiteral("(%1)");
    QString versionString = versionStringTemplate.arg("?");
    Db* db = item->getDb();
    if (!db)
        return;

    if (db->isValid())
    {
        QString t = db->getTypeLabel();
        versionString = versionStringTemplate.arg(t);
    }
    else
    {
        versionString = versionStringTemplate.arg(tr("error", "dbtree labels"));
    }

    paintLabel(painter, option, index, item, versionString);
}

void DbTreeItemDelegate::paintChildCount(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, DbTreeItem *item) const
{
    int cnt = item->rowCount();
    if (cnt > 0)
        paintLabel(painter, option, index, item, QString("(%1)").arg(cnt));
}

void DbTreeItemDelegate::paintTableLabel(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, DbTreeItem* item) const
{
    if (isSystemTable(item->text()))
    {
        paintLabel(painter, option, index, item, tr("(system table)", "database tree label"));
        return;
    }

    if (!CFG_UI.General.ShowRegularTableLabels.get())
        return;

    int columnsCount = item->child(0)->rowCount();
    int indexesCount = item->child(1)->rowCount();
    int triggersCount = item->child(2)->rowCount();
    paintLabel(painter, option, index, item, QString("(%1, %2, %3)").arg(columnsCount).arg(indexesCount).arg(triggersCount));
}

void DbTreeItemDelegate::paintVirtualTableLabel(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, DbTreeItem* item) const
{
    if (!CFG_UI.General.ShowVirtualTableLabels.get())
        return;

    paintLabel(painter, option, index, item, tr("(virtual)", "virtual table label"));
}

void DbTreeItemDelegate::paintSystemIndexLabel(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, DbTreeItem* item) const
{
    Db* db = item->getDb();
    if (!db || !db->isValid())
        return;

    if (!isSystemIndex(item->text(), db->getDialect()))
        return;

    paintLabel(painter, option, index, item, tr("(system index)", "database tree label"));
}

void DbTreeItemDelegate::paintLabel(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, DbTreeItem *item, const QString &label) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    painter->save();

    // Colors
    painter->setPen(CFG_UI.Colors.DbTreeLabelsFg.get());

    // Font
    opt.font = CFG_UI.Fonts.DbTreeLabel.get();
    opt.fontMetrics = QFontMetrics(opt.font);
    painter->setFont(opt.font);

    // Coords
    int x = option.rect.x() + option.fontMetrics.width(item->text()) + 15 + option.decorationSize.width();
    int y = opt.rect.y() + (opt.rect.height() - opt.fontMetrics.descent() - opt.fontMetrics.ascent()) / 2 + opt.fontMetrics.ascent();

    // Paint
    painter->drawText(QPoint(x, y), label);
    painter->restore();
}