00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
#include "treecombobox.h"
00021
00022
#include <qpainter.h>
00023
00024 TreeListBoxItem::TreeListBoxItem(
QListBox *lb,
const QPixmap& pix,
const QString& txt,
bool oneBlock)
00025 :
QListBoxPixmap(pix, txt)
00026 {
00027
if (oneBlock)
00028 m_path =
QStringList(txt);
00029
else
00030 m_path =
QStringList::split(
'/', text(),
false);
00031 m_depth = m_path.count()-1;
00032 m_child = m_next = m_parent = 0;
00033
00034
00035
if (m_depth == 0)
00036 {
00037
TreeListBoxItem *item = static_cast<TreeListBoxItem*>(lb->
item(0));
00038
while (item && item->
m_next)
00039 item = item->
m_next;
00040 lb->
insertItem(
this);
00041
if (item)
00042 item->
m_next =
this;
00043 }
00044
else
00045 {
00046
QString parentStr = txt.
left(txt.
length()-m_path[m_depth].length()-1);
00047
TreeListBoxItem *parentItem = static_cast<TreeListBoxItem*>(lb->
findItem(parentStr, Qt::ExactMatch));
00048
if (!parentItem)
00049 {
00050
00051 parentItem =
new TreeListBoxItem(lb,
QPixmap(), parentStr);
00052 }
00053
00054
00055
TreeListBoxItem *childItem = static_cast<TreeListBoxItem*>(parentItem), *prevItem = 0;
00056
while (childItem->next())
00057 {
00058
TreeListBoxItem *nextItem = static_cast<TreeListBoxItem*>(childItem->next());
00059
if (nextItem->
m_depth >= m_depth)
00060 {
00061 childItem = nextItem;
00062
if (childItem->
m_depth == m_depth)
00063 prevItem = childItem;
00064 }
00065
else
00066
break;
00067 }
00068
00069 lb->
insertItem(
this, childItem);
00070 m_parent = parentItem;
00071
if (prevItem)
00072 prevItem->
m_next =
this;
00073
else
00074 parentItem->
m_child =
this;
00075 }
00076 }
00077
00078
int TreeListBoxItem::width(
const QListBox *lb)
const
00079
{
00080
int w = m_depth * stepSize() + 2;
00081
if (
pixmap())
00082 w += (
pixmap()->width() + 2);
00083
if (!m_path[m_depth].isEmpty())
00084 w += (lb->fontMetrics().width(m_path[m_depth]) + 2);
00085
return QMAX(w, QListBoxPixmap::width(lb));
00086 }
00087
00088
void TreeListBoxItem::paint(
QPainter *p)
00089 {
00090
if(!static_cast<TreeListBox*>(listBox())->m_painting)
00091 {
00092
QListBoxPixmap::paint(p);
00093
return;
00094 }
00095
00096
const QPixmap *pix =
pixmap();
00097
QRect r = p->
viewport();
00098
int h =
height(listBox());
00099
int xo = (m_depth * stepSize() + 2);
00100
int yo = (pix ? (h-pix->
height())/2 : 0);
00101
00102
if (m_depth > 0)
00103 {
00104
QPen oldPen = p->
pen();
00105 p->
setPen(listBox()->colorGroup().mid());
00106
00107
TreeListBoxItem *item =
this;
00108
int s = xo-stepSize()/2;
00109 p->
drawLine(s, r.
top(), s, h/2);
00110 p->
drawLine(s, h/2, s+stepSize()/2-2, h/2);
00111
while (item->
m_parent)
00112 {
00113
if (item->
m_next)
00114 p->
drawLine(s, r.
top(), s, h);
00115 item = item->
m_parent;
00116 s -= stepSize();
00117 }
00118
00119 p->
setPen(oldPen);
00120 }
00121
if (pix)
00122 {
00123 p->
drawPixmap(xo, yo, *pix);
00124 xo += (pix->
width() + 2);
00125 }
00126 p->
drawText(xo, 0, r.
width()-xo,
height(listBox()), Qt::AlignLeft, m_path[m_depth]);
00127 }
00128
00129
00130
00131 TreeListBox::TreeListBox(
QWidget *parent,
const char *name)
00132 :
QListBox(parent, name)
00133 {
00134 m_painting =
false;
00135 }
00136
00137
void TreeListBox::paintCell(
QPainter *p,
int row,
int col)
00138 {
00139 m_painting =
true;
00140
QListBox::paintCell(p, row, col);
00141 m_painting =
false;
00142 }
00143
00144
00145
00146 TreeComboBox::TreeComboBox(
QWidget *parent,
const char *name)
00147 :
QComboBox(parent, name)
00148 {
00149 m_listbox =
new TreeListBox(
this);
00150 setListBox(m_listbox);
00151 }
00152
00153
void TreeComboBox::insertItem(
const QPixmap& pix,
const QString& txt,
bool oneBlock)
00154 {
00155
new TreeListBoxItem(m_listbox, pix, txt, oneBlock);
00156 }