00001
00002
00003 #include "Iguana/Studio/interface/IgQtTwigBrowser.h"
00004 #include "Iguana/Studio/interface/IgQtTwigRep.h"
00005 #include "Iguana/Studio/interface/xtypeinfo.h"
00006 #include "Iguana/Studio/interface/IgQtSite.h"
00007 #include "Iguana/Studio/interface/IgQtObjectMenuService.h"
00008 #include "Iguana/Framework/interface/IgRepSet.h"
00009 #include "Iguana/Framework/interface/IgRepContext.h"
00010 #include "Iguana/Framework/interface/IgSelectionService.h"
00011 #include "Iguana/Framework/interface/IgSelectionMessage.h"
00012 #include "Iguana/Framework/interface/IgBrowserMethods.h"
00013 #include "Iguana/Framework/interface/xtypeinfo.h"
00014 #include <qheader.h>
00015 #include <qevent.h>
00016 #include <qlist.h>
00017 #include <qpopupmenu.h>
00018 #include <algorithm>
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 const char *
00031 IgQtTwigBrowser::catalogLabel (void)
00032 { return "Twig"; }
00033
00034 IgQtTwigBrowser::IgQtTwigBrowser (IgState *state,
00035 IgSite *site)
00036 : QListView (IgQtSite::hostFrom (site)),
00037 m_state (state),
00038 m_discardRelease (false)
00039 {
00040 ASSERT (IgSelectionService::get (m_state));
00041 ASSERT (IgQtObjectMenuService::get (m_state));
00042 IgQtSite::host (site, this);
00043 IgSelectionService::get (m_state)
00044 ->add (lat::CreateCallback (this, &IgQtTwigBrowser::selectMessage));
00045
00046
00047 header ()->setMovingEnabled (false);
00048
00049
00050 setSorting (-1, false);
00051
00052 addColumn ("Object", visibleWidth () - 20);
00053 addColumn ("Visibility", 20);
00054 setVScrollBarMode (QScrollView::AlwaysOn);
00055 resize (200, height ());
00056
00057
00058 setRootIsDecorated (true);
00059 setMultiSelection (false);
00060 connect (this,SIGNAL(selectionChanged()), this,SLOT(onSelectionChange()));
00061 connect (this,SIGNAL(selectionChanged()), this,SLOT(slotItemChanged()));
00062 connect (this,SIGNAL(contextMenuRequested(QListViewItem *,
00063 const QPoint &, int)),
00064 this,SLOT(contextMenu(QListViewItem *, const QPoint &, int)));
00065
00066 show ();
00067 }
00068
00069 IgQtTwigBrowser::~IgQtTwigBrowser (void)
00070 {
00071 IgSelectionService::get (m_state)
00072 ->remove (lat::CreateCallback (this, &IgQtTwigBrowser::selectMessage));
00073 }
00074
00075
00076 void
00077 IgQtTwigBrowser::browse (IgRepresentable *object)
00078 {
00079
00080
00081 IgRepSet::lookup (object, this, true);
00082
00083
00084
00085
00086
00087 }
00088
00089 void
00090 IgQtTwigBrowser::select (IgQtTwigRep *rep)
00091 {
00092
00093 if (! rep)
00094 {
00095 QList<IgQtTwigRep> *selected = getSelectedItems ();
00096 for (IgQtTwigRep *rep = selected->first(); rep; rep = selected->next())
00097 setSelected (rep, false);
00098 delete selected;
00099 }
00100 else
00101 {
00102 if (! isSelected (rep))
00103 setSelected (rep, true);
00104
00105 ensureItemVisible (rep);
00106 }
00107 }
00108
00110 void
00111 IgQtTwigBrowser::selectMessage (IgSelectionMessage message)
00112 {
00113
00114 if (! message.context ())
00115 select (0);
00116 else if (IgQtTwigRep *rep = dynamic_cast<IgQtTwigRep *>
00117 (IgRepSet::lookup (message.context (), this, true)))
00118
00119 select (rep);
00120 }
00121
00125 #if 0
00127 void
00128 IgQtTwigBrowser::checkSelected (void)
00129 {
00130
00131 QList<IgQtTwigRep> *selected = getSelectedItems ();
00132 for (IgQtTwigRep *rep = selected->first (); rep; rep = selected->next())
00133 {
00134
00135
00136 rep->check (true);
00137 }
00138
00139 delete selected;
00140 }
00141
00143 void
00144 IgQtTwigBrowser::updateTree (void)
00145 {
00146
00147
00148 QList<IgQtTwigRep> *selected = getCheckedItems (IgQtTwigRep::VISIBLE_FIELD);
00149 for (IgQtTwigRep *rep = selected->first (); rep; rep = selected->next ())
00150 {
00151
00152
00153
00154
00155 rep->check (rep->checked ());
00156 }
00157
00158
00159
00160 delete selected;
00161 }
00162
00164
00165
00166
00167
00168
00169
00170
00171 #endif // 0
00172
00174 void
00175 IgQtTwigBrowser::onSelectionChange (void)
00176 {
00177
00178 QList<IgQtTwigRep> *selected = getSelectedItems ();
00179 for (IgQtTwigRep *rep = selected->first (); rep; rep = selected->next ())
00180 IgSelectionService::get (m_state)
00181 ->broadcast (IgSelectionMessage (rep->context ()));
00182 delete selected;
00183 }
00184
00185 void
00186 IgQtTwigBrowser::resizeEvent (QResizeEvent *event)
00187 {
00188 QListView::resizeEvent (event);
00189
00190 int diff = event->size().width () - event->oldSize().width ();
00191 if (diff != 0)
00192 {
00193 diff = 0;
00194 for (int i = 1; i < columns (); i++)
00195 diff += columnWidth (i);
00196 setColumnWidth (0, visibleWidth () - diff);
00197 updateContents ();
00198 }
00199 }
00200
00201 void
00202 IgQtTwigBrowser::contentsMouseDoubleClickEvent (QMouseEvent *event)
00203 {
00204 ASSERT (event);
00205
00206 QPoint pos = contentsToViewport (event->pos ());
00207 QListViewItem *item = itemAt (pos);
00208 IgQtTwigRep *rep = static_cast<IgQtTwigRep *> (item);
00209
00210 ASSERT (!item || dynamic_cast<IgQtTwigRep *> (item));
00211
00212 if (rep && rep->magic ())
00213
00214 doubleClicked (rep);
00215 else
00216 QListView::contentsMouseDoubleClickEvent (event);
00217 }
00218
00219 void
00220 IgQtTwigBrowser::contentsMousePressEvent (QMouseEvent *event)
00221 {
00222 ASSERT (event);
00223
00224 QPoint pos = contentsToViewport (event->pos ());
00225 QListViewItem *item = itemAt (pos);
00226 int column = isInsideChecker (pos.x ());
00227
00228 if (item && column != -1)
00229 {
00230
00231
00232 ASSERT (dynamic_cast<IgQtTwigRep *> (item));
00233 IgQtTwigRep *rep = static_cast<IgQtTwigRep *> (item);
00234 rep->check (column, ! rep->checked (column));
00235 m_discardRelease = true;
00236 }
00237 else
00238 {
00239 m_discardRelease = false;
00240 QListView::contentsMousePressEvent (event);
00241 }
00242 }
00243
00244 void
00245 IgQtTwigBrowser::contentsMouseReleaseEvent (QMouseEvent *event)
00246 {
00247 if (m_discardRelease)
00248 m_discardRelease = false;
00249 else
00250 QListView::contentsMouseReleaseEvent (event);
00251 }
00252
00253 int
00254 IgQtTwigBrowser::isInsideChecker (int x)
00255 {
00256
00257 x += contentsX ();
00258
00259
00260
00261
00262 static const int CHECK_COLUMN = IgQtTwigRep::VISIBLE_FIELD;
00263 int i;
00264
00265 for (i = 0; i < CHECK_COLUMN; ++i)
00266 if ((x -= columnWidth (i)) < 0)
00267 return -1;
00268
00269 if ((x -= columnWidth (i)) < 0)
00270 return i;
00271
00272 return -1;
00273 }
00274
00276 void
00277 IgQtTwigBrowser::contextMenu (QListViewItem *item, const QPoint &pos, int)
00278 {
00279 ASSERT (!item || dynamic_cast<IgQtTwigRep *> (item));
00280 if (! item)
00281 return;
00282
00283 IgQtObjectMenuService *svc = IgQtObjectMenuService::get (m_state);
00284 IgRepresentable *object = static_cast<IgQtTwigRep *> (item)
00285 ->context ()->object ();
00286
00287
00288 svc->request (object)->exec (pos);
00289 svc->postUpdate (object);
00290 }
00291
00294 QListViewItem *
00295 IgQtTwigBrowser::getNextUpItem (QListViewItem *item)
00296 {
00297 if (QListViewItem *parent = item->parent ())
00298 if (QListViewItem *next = parent->nextSibling ())
00299 return next;
00300 else
00301 return getNextUpItem (parent);
00302
00303 return 0;
00304 }
00305
00307 QListViewItem *
00308 IgQtTwigBrowser::getNextItem (QListViewItem *item)
00309 {
00310 if (QListViewItem *next = item->firstChild ())
00311 return next;
00312 else if ((next = item->nextSibling ()))
00313 return next;
00314 else
00315 return getNextUpItem (item);
00316 }
00317
00319 QList<IgQtTwigRep> *
00320 IgQtTwigBrowser::getSelectedItems (void)
00321 {
00322 QList<IgQtTwigRep> *selected = new QList<IgQtTwigRep>;
00323 selected->setAutoDelete (false);
00324
00325 for (QListViewItem *item = firstChild (); item; item = getNextItem (item))
00326 if (item->isSelected ())
00327 {
00328 ASSERT (dynamic_cast<IgQtTwigRep *> (item));
00329 IgQtTwigRep *rep = static_cast<IgQtTwigRep *> (item);
00330 selected->append (rep);
00331 }
00332
00333 return selected;
00334 }
00335
00338 QList<IgQtTwigRep> *
00339 IgQtTwigBrowser::getCheckedItems (int column)
00340 {
00341 QList<IgQtTwigRep> *checked = new QList<IgQtTwigRep>;
00342 checked->setAutoDelete (false);
00343
00344 for (QListViewItem *item = firstChild (); item; item = getNextItem (item))
00345 if (static_cast<IgQtTwigRep *> (item)->checked (column))
00346 {
00347 ASSERT (dynamic_cast<IgQtTwigRep *> (item));
00348 IgQtTwigRep *rep = static_cast<IgQtTwigRep *> (item);
00349 checked->append (rep);
00350 }
00351
00352 return checked;
00353 }
00354
00356 void
00357 IgQtTwigBrowser::slotItemChanged (void)
00358 {
00359
00360 }
00361
00362 void
00363 IgQtTwigBrowser::slotItemRMClicked (void)
00364 {
00365
00366 }