Applets
notifierview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "notifierview.h"
00021
00022
00023
00024 #include <QtGui/QMouseEvent>
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QPaintEvent>
00027 #include <QtGui/QScrollBar>
00028 #include <QtGui/QHeaderView>
00029
00030
00031 #include <KDebug>
00032 #include <KIconLoader>
00033
00034
00035 #include <plasma/delegate.h>
00036
00037 using namespace Notifier;
00038
00039 NotifierView::NotifierView(QWidget *parent)
00040 : QTreeView(parent)
00041 {
00042 setIconSize(QSize(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
00043 setRootIsDecorated(false);
00044 setHeaderHidden(true);
00045 setMouseTracking(true);
00046 }
00047
00048 NotifierView::~NotifierView()
00049 {
00050
00051 }
00052
00053 void NotifierView::resizeEvent(QResizeEvent * event)
00054 {
00055
00056
00057 if (header()->count() > 0) {
00058 const int newWidth = event->size().width() -
00059 (header()->count()-1)*(sizeHintForRow(0));
00060 header()->resizeSection(0, newWidth);
00061 }
00062 }
00063
00064 void NotifierView::mouseMoveEvent(QMouseEvent *event)
00065 {
00066 const QModelIndex itemUnderMouse = indexAt(event->pos());
00067 if (itemUnderMouse != m_hoveredIndex && itemUnderMouse.isValid() &&
00068 state() == NoState) {
00069 update(itemUnderMouse);
00070 update(m_hoveredIndex);
00071
00072 m_hoveredIndex = itemUnderMouse;
00073 setCurrentIndex(m_hoveredIndex);
00074 } else if (!itemUnderMouse.isValid()) {
00075 m_hoveredIndex = QModelIndex();
00076 setCurrentIndex(m_hoveredIndex);
00077 }
00078
00079 QAbstractItemView::mouseMoveEvent(event);
00080 }
00081
00082 void NotifierView::leaveEvent(QEvent *event)
00083 {
00084 const QModelIndex oldHoveredIndex = m_hoveredIndex;
00085 m_hoveredIndex = QModelIndex();
00086 setCurrentIndex(m_hoveredIndex);
00087 update(oldHoveredIndex);
00088 }
00089
00090 QModelIndex NotifierView::moveCursor(CursorAction cursorAction,Qt::KeyboardModifiers modifiers )
00091 {
00092 m_hoveredIndex = QModelIndex();
00093
00094 return QTreeView::moveCursor(cursorAction, modifiers );
00095 }
00096
00097 void NotifierView::paintEvent(QPaintEvent *event)
00098 {
00099 if (!model()) {
00100 return;
00101 }
00102
00103 QPainter painter(viewport());
00104 painter.setRenderHint(QPainter::Antialiasing);
00105
00106 const int rows = model()->rowCount(rootIndex());
00107 const int cols = header()->count();
00108
00109
00110 for (int i = 0; i < rows; ++i) {
00111 for (int j = 0; j < cols; ++j) {
00112 const QModelIndex index = model()->index(i, j, rootIndex());
00113 const QRect itemRect = visualRect(index);
00114
00115 if (event->region().contains(itemRect)) {
00116 QStyleOptionViewItem option = viewOptions();
00117 option.rect = itemRect;
00118
00119 if (selectionModel()->isSelected(index)) {
00120 option.state |= QStyle::State_Selected;
00121 }
00122 if (index == m_hoveredIndex) {
00123 option.state |= QStyle::State_MouseOver;
00124 }
00125 if (index == currentIndex()) {
00126 option.state |= QStyle::State_HasFocus;
00127 }
00128
00129 itemDelegate(index)->paint(&painter,option,index);
00130 }
00131 }
00132 }
00133 }
00134
00135 #include "notifierview.moc"