Changeset View
Changeset View
Standalone View
Standalone View
processui/ksysguardprocesslist.cpp
Show First 20 Lines • Show All 75 Lines • ▼ Show 20 Line(s) | 74 | public: | |||
---|---|---|---|---|---|
76 | } | 76 | } | ||
77 | 77 | | |||
78 | virtual void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const | 78 | virtual void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const | ||
79 | { | 79 | { | ||
80 | QStyleOptionViewItemV4 option = opt; | 80 | QStyleOptionViewItemV4 option = opt; | ||
81 | initStyleOption(&option,index); | 81 | initStyleOption(&option,index); | ||
82 | 82 | | |||
83 | float percentage = index.data(ProcessModel::PercentageRole).toFloat(); | 83 | float percentage = index.data(ProcessModel::PercentageRole).toFloat(); | ||
84 | if (percentage >= 0) | 84 | auto history = index.data(ProcessModel::PercentageHistoryRole).value<QVector<ProcessModel::PercentageHistoryEntry>>(); | ||
85 | drawPercentageDisplay(painter,option, percentage); | 85 | if (percentage >= 0 || history.size() > 1) | ||
86 | drawPercentageDisplay(painter, option, percentage, history); | ||||
86 | else | 87 | else | ||
87 | QStyledItemDelegate::paint(painter, option, index); | 88 | QStyledItemDelegate::paint(painter, option, index); | ||
88 | } | 89 | } | ||
89 | 90 | | |||
90 | private: | 91 | private: | ||
91 | inline void drawPercentageDisplay(QPainter *painter, QStyleOptionViewItemV4 &option, float percentage) const | 92 | inline void drawPercentageDisplay(QPainter *painter, QStyleOptionViewItemV4 &option, float percentage, const QVector<ProcessModel::PercentageHistoryEntry> &history) const | ||
92 | { | 93 | { | ||
93 | QStyle *style = option.widget ? option.widget->style() : QApplication::style(); | 94 | QStyle *style = option.widget ? option.widget->style() : QApplication::style(); | ||
95 | const QRect &rect = option.rect; | ||||
96 | | ||||
97 | const int HIST_MS_PER_PX = 100; // 100 ms = 1 px -> 1 s = 10 px | ||||
98 | bool hasHistory = history.size() > 1; | ||||
99 | // Make sure that more than one entry is visible | ||||
100 | if (hasHistory) { | ||||
101 | int width = (history.crbegin()->timestamp - (history.crbegin() + 1)->timestamp) / HIST_MS_PER_PX; | ||||
102 | hasHistory = width < rect.width(); | ||||
103 | } | ||||
94 | 104 | | |||
95 | // draw the background | 105 | // draw the background | ||
96 | style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); | 106 | style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); | ||
97 | 107 | | |||
98 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled | 108 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled | ||
99 | ? QPalette::Normal : QPalette::Disabled; | 109 | ? QPalette::Normal : QPalette::Disabled; | ||
100 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) | 110 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) | ||
101 | cg = QPalette::Inactive; | 111 | cg = QPalette::Inactive; | ||
102 | 112 | | |||
103 | //Now draw our percentage thingy | 113 | //Now draw our percentage thingy | ||
104 | const QRect &rect = option.rect; | 114 | int size = qMin(int(percentage * rect.height()), rect.height()); | ||
105 | int size = qMin(percentage,1.0f) * rect.width(); | | |||
106 | if(size > 2 ) { //make sure the line will have a width of more than 1 pixel | 115 | if(size > 2 ) { //make sure the line will have a width of more than 1 pixel | ||
107 | painter->setPen(Qt::NoPen); | 116 | painter->setPen(Qt::NoPen); | ||
108 | QColor color = option.palette.color(cg, QPalette::Link); | 117 | QColor color = option.palette.color(cg, QPalette::Link); | ||
109 | color.setAlpha(50); | 118 | color.setAlpha(33); | ||
119 | | ||||
120 | painter->fillRect( rect.x(), rect.y() + rect.height() - size, rect.width(), size, color); | ||||
121 | } | ||||
122 | | ||||
123 | // Draw the history graph | ||||
124 | if(hasHistory) | ||||
broulik: Coding style | |||||
125 | { | ||||
126 | QColor color = option.palette.color(cg, QPalette::Link); | ||||
127 | color.setAlpha(66); | ||||
128 | painter->setPen(Qt::NoPen); | ||||
129 | | ||||
130 | QPainterPath path; | ||||
131 | // From right to left | ||||
132 | path.moveTo(rect.right(), rect.bottom()); | ||||
133 | | ||||
134 | int xNow = rect.right(); | ||||
135 | auto now = history.constLast(); | ||||
136 | int height = qMin(int(rect.height() * now.value), rect.height()); | ||||
137 | path.lineTo(xNow, rect.bottom() - height); | ||||
138 | | ||||
139 | for(int index = history.size() - 2; index >= 0 && xNow > rect.left(); --index) { | ||||
140 | auto next = history.at(index); | ||||
141 | int width = (now.timestamp - next.timestamp) / HIST_MS_PER_PX; | ||||
142 | int xNext = qMax(xNow - width, rect.left()); | ||||
143 | | ||||
144 | now = next; | ||||
145 | xNow = xNext; | ||||
146 | int height = qMin(int(rect.height() * now.value), rect.height()); | ||||
147 | | ||||
148 | path.lineTo(xNow, rect.bottom() - height); | ||||
149 | } | ||||
150 | | ||||
151 | path.lineTo(xNow, rect.bottom()); | ||||
152 | path.lineTo(rect.right(), rect.bottom()); | ||||
110 | 153 | | |||
111 | painter->fillRect( rect.x(), rect.y(), size, rect.height(), color); | 154 | painter->fillPath(path, color); | ||
112 | } | 155 | } | ||
113 | 156 | | |||
114 | // draw the text | 157 | // draw the text | ||
115 | if (!option.text.isEmpty()) { | 158 | if (!option.text.isEmpty()) { | ||
116 | QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &option, option.widget); | 159 | QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &option, option.widget); | ||
117 | 160 | | |||
118 | 161 | | |||
119 | if (option.state & QStyle::State_Selected) { | 162 | if (option.state & QStyle::State_Selected) { | ||
▲ Show 20 Lines • Show All 1363 Lines • Show Last 20 Lines |
Coding style