00001
00015 #include <qglobal.h>
00016 #include <qpainter.h>
00017 #include <qframe.h>
00018 #include <qpoint.h>
00019 #include <qcursor.h>
00020
00021 #include "poplistview.h"
00022
00023
00024 ColorListViewItem::ColorListViewItem(QListView* parent, const QColor& color,
00025 bool separator)
00026 : QListViewItem(parent), m_separator(separator)
00027 {
00028 init(parent, color);
00029 }
00030
00031
00032 ColorListViewItem::ColorListViewItem(QListView* parent, QListViewItem* after,
00033 const QColor& color, bool separator)
00034 : QListViewItem(parent, after), m_separator(separator)
00035 {
00036 init(parent, color);
00037 }
00038
00039
00040 ColorListViewItem::ColorListViewItem(QListViewItem* parent, const QColor& color,
00041 bool separator)
00042 : QListViewItem(parent), m_separator(separator)
00043 {
00044 init(parent->listView(), color);
00045 }
00046
00047
00048 ColorListViewItem::ColorListViewItem(QListViewItem* parent, QListViewItem* after,
00049 const QColor& color, bool separator)
00050 : QListViewItem(parent, after), m_separator(separator)
00051 {
00052 init(parent->listView(), color);
00053 }
00054
00055
00056 ColorListViewItem::~ColorListViewItem()
00057 {
00058 }
00059
00060
00061 void ColorListViewItem::init(QListView* parent, const QColor& color)
00062 {
00063 m_listView = parent;
00064 if(color.isValid())
00065 m_color = color;
00066 else
00067 m_color = m_listView->colorGroup().color(QColorGroup::Text);
00068 }
00069
00070
00071 void ColorListViewItem::paintCell(QPainter* p, const QColorGroup& cg,
00072 int column, int width, int align)
00073 {
00074 QColorGroup my_cg(cg);
00075
00076 my_cg.setColor(QColorGroup::Text, m_color);
00077 QListViewItem::paintCell(p, my_cg, column, width, align);
00078
00079 if(dynamic_cast<popListView*>(m_listView)->isGridEnabled())
00080 {
00081 p->setPen( cg.mid() );
00082 p->moveTo(0, height()-1);
00083 p->lineTo(width-1, height()-1);
00084 p->lineTo(width-1, 0);
00085 if(m_separator)
00086 {
00087 QPen pen;
00088 pen.setWidth(2);
00089 p->setPen(pen);
00090 p->moveTo(0, 0);
00091 p->lineTo(width-1, 0);
00092 }
00093 }
00094 }
00095
00096
00097 popListView::popListView(QWidget *parent, bool grid, const char *name)
00098 :QListView(parent,name), drawGrid(grid)
00099 {
00100 setAllColumnsShowFocus(TRUE);
00101 popup = new KPopupMenu(this);
00102 triggerUpdate();
00103
00104 lastRootItem = 0;
00105 lastChildItem = 0;
00106
00107 setFrameStyle( QFrame::Box | QFrame::Sunken );
00108 setLineWidth( 1 );
00109 setMidLineWidth( 0 );
00110
00111 if(QT_VERSION < 0x030005)
00112 {
00113 connect(this,
00114 SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint&, int)),
00115 SLOT(slotMouseButtonClicked(int, QListViewItem*, const QPoint&, int)));
00116 }
00117 else
00118 {
00119 connect(this,
00120 SIGNAL(contextMenuRequested(QListViewItem*, const QPoint&, int)),
00121 SLOT(slotShowContextMenu(QListViewItem*, const QPoint&, int)));
00122 }
00123 }
00124
00125
00126 popListView::~popListView()
00127 {
00128 }
00129
00130
00131 void popListView::setGridEnabled(bool grid)
00132 {
00133 drawGrid = grid;
00134 update();
00135 }
00136
00137
00138 QListViewItem* popListView::insertRootItem(const QString& aName,
00139 QPixmap* icon)
00140 {
00141 QListViewItem* item;
00142
00143
00144 if( lastRootItem != 0 )
00145 {
00146 item = new ColorListViewItem( this, lastRootItem );
00147 item->setText( 0, aName );
00148 }
00149 else
00150 {
00151 item = new ColorListViewItem( this );
00152 item->setText( 0, aName );
00153 }
00154
00155 if(icon != 0)
00156 item->setPixmap(0, *icon );
00157
00158
00159 lastRootItem = item;
00160
00161 return item;
00162 }
00163
00164
00165 QListViewItem* popListView::insertChildItem(const QString& aName,
00166 QPixmap* icon,
00167 QListViewItem *parent)
00168 {
00169 QListViewItem *item;
00170
00171 if(parent != 0)
00172 {
00173 item = new ColorListViewItem( parent, lastChildItem );
00174 item->setText( 0, aName );
00175 }
00176 else
00177 {
00178 item = new QListViewItem( lastRootItem, lastChildItem );
00179 item->setText( 0, aName );
00180 }
00181 if(icon != 0)
00182 item->setPixmap( 0, *icon );
00183
00184
00185 lastChildItem = item;
00186
00187 return item;
00188 }
00189
00190
00191 void popListView::clear()
00192 {
00193 lastRootItem = 0;
00194 lastChildItem = 0;
00195 QListView::clear();
00196 }
00197
00198
00199 int popListView::insertPopupItem(const QString& name, int id, int index)
00200 {
00201 return(popup->insertItem(name, id, index));
00202 }
00203
00204
00205 int popListView::insertPopupItem(const QPixmap& icon, const QString& name,
00206 int id, int index)
00207 {
00208 return(popup->insertItem(QIconSet(icon, QIconSet::Small), name, id, index));
00209 }
00210
00211
00212 int popListView::insertPopupItem(const QString& name, QPopupMenu* pop,
00213 int id, int index)
00214 {
00215 return(popup->insertItem(name, pop, id, index));
00216 }
00217
00218
00219 int popListView::insertPopupItem(const QPixmap& icon, const QString& name,
00220 QPopupMenu* pop, int id, int index)
00221 {
00222 return(popup->insertItem(QIconSet(icon, QIconSet::Small), name, pop,
00223 id, index));
00224 }
00225
00226
00227 void popListView::slotMouseButtonClicked(int but, QListViewItem* it,
00228 const QPoint&, int)
00229 {
00230 if(it == 0 && pstyle == ShowAnywhere)
00231 {
00232 setSelected(currentItem(), false);
00233 emit selectionChanged(0);
00234 }
00235
00236 if(but == Qt::RightButton && popup->count() > 0)
00237 {
00238 if(pstyle == ShowAnywhere || (it && pstyle == OverItem))
00239 popup->popup(QCursor::pos());
00240 }
00241 }
00242
00243
00244 void popListView::slotShowContextMenu(QListViewItem* it, const QPoint& pos,
00245 int col)
00246 {
00247 slotMouseButtonClicked(Qt::RightButton, it, pos, col);
00248 }
00249
00250
00251 KPopupMenu *popListView::popupMenu()
00252 {
00253 return(popup);
00254 }
00255
00256
00257 void popListView::setPopupStyle(PopupStyle p)
00258 {
00259 pstyle = p;
00260 }
00261
00262
00263 popListView::PopupStyle popListView::popupStyle()
00264 {
00265 return pstyle;
00266 }
00267
00268 #include "poplistview.moc"
00269