superkaramba
kwidgetlistbox.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 "kwidgetlistbox.h"
00021
00022 #include <QApplication>
00023
00024 KWidgetListbox::KWidgetListbox(QWidget *parent)
00025 : QTableWidget(parent)
00026 {
00027 setRowCount(0);
00028 setColumnCount(1);
00029
00030 horizontalHeader()->hide();
00031 verticalHeader()->hide();
00032
00033 setSelectionMode(QAbstractItemView::SingleSelection);
00034
00035 connect(this, SIGNAL(cellClicked(int, int)),
00036 this, SLOT(selectionChanged(int, int)));
00037
00038 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00039
00040 horizontalHeader()->setStretchLastSection(true);
00041 setAlternatingRowColors(true);
00042 }
00043
00044 KWidgetListbox::~KWidgetListbox()
00045 {
00046 clear();
00047 }
00048
00049 void KWidgetListbox::clear()
00050 {
00051 QTableWidget::clear();
00052 }
00053
00054 int KWidgetListbox::insertItem(QWidget* item, int index)
00055 {
00056 int row = 0;
00057 if (index == -1) {
00058 row = rowCount();
00059 setRowCount(row + 1);
00060 } else
00061 return -1;
00062
00063 setRowHeight(row, item->height());
00064 setCellWidget(row, 0, item);
00065
00066 return row;
00067 }
00068
00069 void KWidgetListbox::setSelected(QWidget* item)
00070 {
00071 setSelected(index(item));
00072 }
00073
00074 void KWidgetListbox::selectionChanged(int row, int column)
00075 {
00076 setCurrentCell(row, column);
00077 emit selected(row);
00078 }
00079
00080 void KWidgetListbox::removeItem(QWidget* item)
00081 {
00082 removeItem(index(item));
00083 }
00084
00085 void KWidgetListbox::removeItem(int index)
00086 {
00087 removeRow(index);
00088 }
00089
00090 void KWidgetListbox::setSelected(int index)
00091 {
00092 setCurrentCell(index, 0);
00093 }
00094
00095 int KWidgetListbox::selected() const
00096 {
00097 return currentRow();
00098 }
00099
00100 QWidget* KWidgetListbox::selectedItem() const
00101 {
00102 return item(selected());
00103 }
00104
00105 QWidget* KWidgetListbox::item(int index) const
00106 {
00107 return cellWidget(index, 0);
00108 }
00109
00110 int KWidgetListbox::index(QWidget* itm) const
00111 {
00112 for (int i = 0; i < rowCount(); ++i)
00113 if (item(i) == itm)
00114 return i;
00115 return -1;
00116 }
00117
00118 void KWidgetListbox::showItems(show_callback func, void* data)
00119 {
00120 for (int i = 0; i < rowCount(); ++i) {
00121 if (func == 0)
00122 showRow(i);
00123 else {
00124 if (func(i, item(i), data))
00125 showRow(i);
00126 else
00127 hideRow(i);
00128 }
00129 }
00130 }
00131
00132 void KWidgetListbox::showEvent(QShowEvent*)
00133 {
00134 repaint();
00135 }
00136
00137 void KWidgetListbox::mousePressEvent(QMouseEvent *event)
00138 {
00139 if(event->button() == Qt::LeftButton) {
00140 m_dragStartPosition = event->pos();
00141 }
00142
00143 QTableWidget::mousePressEvent(event);
00144 }
00145
00146 void KWidgetListbox::mouseMoveEvent(QMouseEvent *event)
00147 {
00148 if(!(event->buttons() & Qt::LeftButton)) {
00149 return;
00150 }
00151
00152 if ((event->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
00153 return;
00154 }
00155
00156 if (selected() <= 1) {
00157 return;
00158 }
00159
00160 ThemeWidget *theme = qobject_cast<ThemeWidget*>(selectedItem());
00161 QDrag *drag = new QDrag(this);
00162 QMimeData *mimeData = new QMimeData;
00163
00164
00165 drag->setMimeData(mimeData);
00166 drag->setPixmap(theme->icon());
00167
00168 drag->start(Qt::IgnoreAction);
00169
00170 emit itemDropped(QCursor::pos(), theme);
00171 }
00172
00173 #include "kwidgetlistbox.moc"