KDEUI
kcategorydrawer.cpp
Go to the documentation of this file.
00001 00021 #include "kcategorydrawer.h" 00022 00023 #include <QPainter> 00024 #include <QStyleOption> 00025 #include <QApplication> 00026 00027 #include <kiconloader.h> 00028 #include <kcategorizedview.h> 00029 #include <kcategorizedsortfilterproxymodel.h> 00030 00031 #define HORIZONTAL_HINT 3 00032 00033 class KCategoryDrawer::Private 00034 { 00035 public: 00036 Private() 00037 : leftMargin(0) 00038 , rightMargin(0) 00039 { 00040 } 00041 00042 ~Private() 00043 { 00044 } 00045 00046 int leftMargin; 00047 int rightMargin; 00048 }; 00049 00050 KCategoryDrawer::KCategoryDrawer() 00051 : d(new Private) 00052 { 00053 setLeftMargin(2); 00054 setRightMargin(2); 00055 } 00056 00057 KCategoryDrawer::~KCategoryDrawer() 00058 { 00059 delete d; 00060 } 00061 00062 void KCategoryDrawer::drawCategory(const QModelIndex &index, 00063 int /*sortRole*/, 00064 const QStyleOption &option, 00065 QPainter *painter) const 00066 { 00067 painter->setRenderHint(QPainter::Antialiasing); 00068 00069 const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); 00070 const QRect optRect = option.rect; 00071 QFont font(QApplication::font()); 00072 font.setBold(true); 00073 const QFontMetrics fontMetrics = QFontMetrics(font); 00074 00075 QColor outlineColor = option.palette.text().color(); 00076 outlineColor.setAlphaF(0.35); 00077 00078 //BEGIN: top left corner 00079 { 00080 painter->save(); 00081 painter->setPen(outlineColor); 00082 const QPointF topLeft(optRect.topLeft()); 00083 QRectF arc(topLeft, QSizeF(4, 4)); 00084 arc.translate(0.5, 0.5); 00085 painter->drawArc(arc, 1440, 1440); 00086 painter->restore(); 00087 } 00088 //END: top left corner 00089 00090 //BEGIN: left vertical line 00091 { 00092 QPoint start(optRect.topLeft()); 00093 start.ry() += 3; 00094 QPoint verticalGradBottom(optRect.topLeft()); 00095 verticalGradBottom.ry() += fontMetrics.height() + 5; 00096 QLinearGradient gradient(start, verticalGradBottom); 00097 gradient.setColorAt(0, outlineColor); 00098 gradient.setColorAt(1, Qt::transparent); 00099 painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient); 00100 } 00101 //END: left vertical line 00102 00103 //BEGIN: horizontal line 00104 { 00105 QPoint start(optRect.topLeft()); 00106 start.rx() += 3; 00107 QPoint horizontalGradTop(optRect.topLeft()); 00108 horizontalGradTop.rx() += optRect.width() - 6; 00109 painter->fillRect(QRect(start, QSize(optRect.width() - 6, 1)), outlineColor); 00110 } 00111 //END: horizontal line 00112 00113 //BEGIN: top right corner 00114 { 00115 painter->save(); 00116 painter->setPen(outlineColor); 00117 QPointF topRight(optRect.topRight()); 00118 topRight.rx() -= 4; 00119 QRectF arc(topRight, QSizeF(4, 4)); 00120 arc.translate(0.5, 0.5); 00121 painter->drawArc(arc, 0, 1440); 00122 painter->restore(); 00123 } 00124 //END: top right corner 00125 00126 //BEGIN: right vertical line 00127 { 00128 QPoint start(optRect.topRight()); 00129 start.ry() += 3; 00130 QPoint verticalGradBottom(optRect.topRight()); 00131 verticalGradBottom.ry() += fontMetrics.height() + 5; 00132 QLinearGradient gradient(start, verticalGradBottom); 00133 gradient.setColorAt(0, outlineColor); 00134 gradient.setColorAt(1, Qt::transparent); 00135 painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient); 00136 } 00137 //END: right vertical line 00138 00139 //BEGIN: text 00140 { 00141 QRect textRect(option.rect); 00142 textRect.setTop(textRect.top() + 7); 00143 textRect.setLeft(textRect.left() + 7); 00144 textRect.setHeight(fontMetrics.height()); 00145 textRect.setRight(textRect.right() - 7); 00146 00147 painter->save(); 00148 painter->setFont(font); 00149 QColor penColor(option.palette.text().color()); 00150 penColor.setAlphaF(0.6); 00151 painter->setPen(penColor); 00152 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category); 00153 painter->restore(); 00154 } 00155 //END: text 00156 } 00157 00158 int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const 00159 { 00160 Q_UNUSED(index); 00161 Q_UNUSED(option) 00162 00163 QFont font(QApplication::font()); 00164 font.setBold(true); 00165 QFontMetrics fontMetrics(font); 00166 00167 const int height = fontMetrics.height() + 1 /* 1 pixel-width gradient */ 00168 + 11 /* top and bottom separation */; 00169 return height; 00170 } 00171 00172 int KCategoryDrawer::leftMargin() const 00173 { 00174 return d->leftMargin; 00175 } 00176 00177 void KCategoryDrawer::setLeftMargin(int leftMargin) 00178 { 00179 d->leftMargin = leftMargin; 00180 } 00181 00182 int KCategoryDrawer::rightMargin() const 00183 { 00184 return d->rightMargin; 00185 } 00186 00187 void KCategoryDrawer::setRightMargin(int rightMargin) 00188 { 00189 d->rightMargin = rightMargin; 00190 } 00191 00192 KCategoryDrawer &KCategoryDrawer::operator=(const KCategoryDrawer &cd) 00193 { 00194 d->leftMargin = cd.d->leftMargin; 00195 d->rightMargin = cd.d->rightMargin; 00196 return *this; 00197 } 00198 00199 KCategoryDrawerV2::KCategoryDrawerV2(QObject *parent) 00200 : QObject(parent) 00201 , KCategoryDrawer() 00202 { 00203 } 00204 00205 KCategoryDrawerV2::~KCategoryDrawerV2() 00206 { 00207 } 00208 00209 void KCategoryDrawerV2::mouseButtonPressed(const QModelIndex&, QMouseEvent *event) 00210 { 00211 event->ignore(); 00212 } 00213 00214 void KCategoryDrawerV2::mouseButtonReleased(const QModelIndex&, QMouseEvent *event) 00215 { 00216 event->ignore(); 00217 } 00218 00219 void KCategoryDrawerV2::mouseButtonMoved(const QModelIndex&, QMouseEvent *event) 00220 { 00221 event->ignore(); 00222 } 00223 00224 void KCategoryDrawerV2::mouseButtonDoubleClicked(const QModelIndex&, QMouseEvent *event) 00225 { 00226 event->ignore(); 00227 } 00228 00229 class KCategoryDrawerV3::Private 00230 { 00231 public: 00232 Private(KCategorizedView *view) 00233 : view(view) 00234 { 00235 } 00236 00237 ~Private() 00238 { 00239 } 00240 00241 KCategorizedView *view; 00242 }; 00243 00244 KCategoryDrawerV3::KCategoryDrawerV3(KCategorizedView *view) 00245 : KCategoryDrawerV2(view) 00246 , d(new Private(view)) 00247 { 00248 } 00249 00250 KCategoryDrawerV3::~KCategoryDrawerV3() 00251 { 00252 delete d; 00253 } 00254 00255 KCategorizedView *KCategoryDrawerV3::view() const 00256 { 00257 return d->view; 00258 } 00259 00260 void KCategoryDrawerV3::mouseButtonPressed(const QModelIndex&, const QRect&, QMouseEvent *event) 00261 { 00262 event->ignore(); 00263 } 00264 00265 void KCategoryDrawerV3::mouseButtonReleased(const QModelIndex&, const QRect&, QMouseEvent *event) 00266 { 00267 event->ignore(); 00268 } 00269 00270 void KCategoryDrawerV3::mouseMoved(const QModelIndex&, const QRect&, QMouseEvent *event) 00271 { 00272 event->ignore(); 00273 } 00274 00275 void KCategoryDrawerV3::mouseButtonDoubleClicked(const QModelIndex&, const QRect&, QMouseEvent *event) 00276 { 00277 event->ignore(); 00278 } 00279 00280 void KCategoryDrawerV3::mouseLeft(const QModelIndex&, const QRect&) 00281 { 00282 } 00283 00284 #include "kcategorydrawer.moc"
KDE 4.6 API Reference