1 /* File: "guideuiinspector.cpp", Time-stamp: <2005-04-28 23:41:22 feeley> */
2 
3 /* Copyright (C) 1994-2005 by Marc Feeley, All Rights Reserved. */
4 
5 /*---------------------------------------------------------------------------*/
6 
7 #include "guide.h"
8 #include "guideuiinspector.h"
9 
10 #include <qapplication.h>
11 #include <qmainwindow.h>
12 #include <qstyle.h>
13 #include <qheader.h>
14 
15 /*---------------------------------------------------------------------------*/
16 
GuideUiInspector(QWidget * parent,const char * name,WFlags f)17 GuideUiInspector::GuideUiInspector (QWidget * parent, const char *name, WFlags f)
18   : QListView (parent, name, f)
19 {
20   QHeader *h = header ();
21   h->setMovingEnabled (false);
22 
23   setFont (QFont ("courier", 12));
24   setSelectionMode (Single);
25   setAllColumnsShowFocus (true);
26   setSorting (-1);
27 
28   highlight_enabled = false;
29   selectedRow = -1;
30 
31   scmobj = ___FAL;
32 
33   connect (this, SIGNAL(clicked(QListViewItem*)),
34            this, SLOT(item_clicked(QListViewItem*)));
35 }
36 
clearSelection()37 void GuideUiInspector::clearSelection ()
38 {
39 }
40 
item_clicked(QListViewItem * item)41 void GuideUiInspector::item_clicked (QListViewItem *item)
42 {
43   if (item == 0)
44     return;
45 
46   int row = ((GuideUiInspectorItem*)item)->row;
47   if (row != selectedRow)
48     {
49       guide_inspector_current_changed (scmobj, selectedRow);
50       selectedRow = row;
51     }
52   rowChanged (row);
53 }
54 
set_nb_cols(int nb_cols)55 void GuideUiInspector::set_nb_cols (int nb_cols)
56 {
57   int n = columns ();
58 
59   if (n > nb_cols)
60     {
61       while (n > nb_cols)
62         {
63           n--;
64           removeColumn (n);
65         }
66     }
67   else if (n < nb_cols)
68     {
69       while (n < nb_cols)
70         {
71           addColumn (QString::null, 0);
72           header ()->setClickEnabled (false, n);
73           n++;
74         }
75     }
76 
77   setResizeMode (LastColumn);
78 }
79 
set_column(int col,QString text,int width)80 void GuideUiInspector::set_column (int col, QString text, int width)
81 {
82   while (col >= columns ())
83     addColumn ("");
84   setColumnText (col, text);
85   if (width > 0)
86     setColumnWidth (col, width);
87   setColumnWidthMode (col, Manual);
88 }
89 
set_column_done()90 void GuideUiInspector::set_column_done ()
91 {
92   setResizeMode (LastColumn);
93 }
94 
set_nb_rows(int nb_rows)95 void GuideUiInspector::set_nb_rows (int nb_rows)
96 {
97   int n = childCount ();
98 
99   if (n > nb_rows)
100     {
101       while (n > nb_rows)
102         {
103           n--;
104           delete at (n);
105         }
106     }
107   else if (n < nb_rows)
108     {
109       while (n < nb_rows)
110         {
111           GuideUiInspectorItem *i;
112           if (n > 0)
113             {
114               i = new GuideUiInspectorItem (this, at (n-1));
115               i->row = n;
116             }
117           else
118             {
119               i = new GuideUiInspectorItem (this);
120               i->row = n;
121             }
122           n++;
123         }
124     }
125   if (selectedRow >= nb_rows)
126     selectedRow = -1;
127 }
128 
at(int i)129 GuideUiInspectorItem *GuideUiInspector::at (int i)
130 {
131   QListViewItem *p = firstChild ();
132 
133   while (i > 0 && p != 0)
134     {
135       p = p->nextSibling ();
136       i--;
137     }
138 
139   return (GuideUiInspectorItem*)p;
140 }
141 
set_cell(int row,int col,QString text,bool read_only)142 void GuideUiInspector::set_cell (int row, int col, QString text, bool read_only)
143 {
144   if (row >= childCount ())
145     set_nb_rows(3 * childCount () + 3);
146 
147   GuideUiInspectorItem *i = at (row);
148   if (text == i->text (col)) return;
149   i->setText (col, text);
150   i->setRenameEnabled (col, !read_only);
151 }
152 
set_current(int row,bool highlight_enable)153 void GuideUiInspector::set_current (int row, bool highlight_enable)
154 {
155   GuideUiInspectorItem *i = at (row);
156 
157   if (i == 0)
158     return;
159 
160   if (selectedRow != row)
161     {
162       selectedRow = row;
163       setSelected (i, true);
164     }
165 
166   if (!highlight_enable)
167     QListView::clearSelection ();
168 
169   highlight_enabled = highlight_enable;
170 }
171 
paintCell(QPainter * p,const QColorGroup & cg,int column,int width,int align)172 void GuideUiInspectorItem::paintCell (QPainter *p, const QColorGroup &cg, int column, int width, int align)
173 {
174   bool highlight_enabled = ((GuideUiInspector*)listView ())->highlight_enabled;
175   bool change_base = (row % 2 == 1);
176   bool change_highlight = isSelected () || !highlight_enabled;
177 
178   if (change_base || change_highlight)
179     {
180       QColorGroup _cg (cg);
181       QColor cb = _cg.base ();
182       QColor ch = _cg.highlight ();
183       QColor cht = _cg.highlightedText ();
184 
185       if (change_base)
186         _cg.setColor (QColorGroup::Base, QColor(220, 255, 255));
187 
188       if (change_highlight)
189         {
190           if (highlight_enabled)
191             _cg.setColor (QColorGroup::Highlight, QColor(255, 255, 24));
192           else
193             _cg.setColor (QColorGroup::Highlight, _cg.base ());
194           _cg.setColor (QColorGroup::HighlightedText, QColor(0, 0, 0));
195         }
196 
197       QListViewItem::paintCell (p, _cg, column, width, align);
198 
199       if (change_base)
200         _cg.setColor (QColorGroup::Base, cb);
201 
202       if (change_highlight)
203         {
204           _cg.setColor (QColorGroup::Highlight, ch);
205           _cg.setColor (QColorGroup::HighlightedText, cht);
206         }
207     }
208   else
209     QListViewItem::paintCell (p, cg, column, width, align);
210 }
211 
212 /*---------------------------------------------------------------------------*/
213 
214 /* Local Variables: */
215 /* mode: C++ */
216 /* End: */
217