CMS 3D CMS Logo

IgQtTwigBrowser.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
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 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00021 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00022 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00023 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00024 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00025 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00026 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00027 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00028 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
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     //header ()->setResizeEnabled (false);
00047     header ()->setMovingEnabled (false);
00048 
00049     // FIXME: enable sorting, but default to off!
00050     setSorting (-1, false);
00051 
00052     addColumn ("Object", visibleWidth () - 20);
00053     addColumn ("Visibility", 20);
00054     setVScrollBarMode (QScrollView::AlwaysOn);
00055     resize (200, height ());
00056     
00057     // addColumn ("Type");
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     // FIXME: This was stateless, no rep created, need to change that.
00080     // FIXME: broadcast selection message
00081     /* IgRepContext *context =*/ IgRepSet::lookup (object, this, true);
00082     // FIXME: model change event?
00083     // FIXME: select?
00084     // if (context)
00085     //     IgSelectionService::get (m_state)
00086     //          ->broadcast (IgSelectionMessage (context));
00087 }
00088 
00089 void
00090 IgQtTwigBrowser::select (IgQtTwigRep *rep)
00091 {
00092     // FIXME: protect against looping from signals?
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     // FIXME: avoid re-emitting the signal?
00114     if (! message.context ())
00115         select (0);
00116     else if (IgQtTwigRep *rep = dynamic_cast<IgQtTwigRep *>
00117              (IgRepSet::lookup (message.context (), this, true)))
00118         // FIXME: update display
00119         select (rep);
00120 }
00121 
00125 #if 0
00127 void
00128 IgQtTwigBrowser::checkSelected (void)
00129 {
00130     // FIXME: gets called for saving
00131     QList<IgQtTwigRep> *selected = getSelectedItems ();
00132     for (IgQtTwigRep *rep = selected->first (); rep; rep = selected->next())
00133     {
00134         // this will automatically set the visibility, emit the signals
00135         // broadcast a change message and update all other browsers
00136         rep->check (true);
00137     }
00138 
00139     delete selected;
00140 }
00141 
00143 void
00144 IgQtTwigBrowser::updateTree (void)
00145 {
00146     // FIXME: gets called after opening a file
00147     // FIXME: this should be handled in the application data logic!
00148     QList<IgQtTwigRep> *selected = getCheckedItems (IgQtTwigRep::VISIBLE_FIELD);
00149     for (IgQtTwigRep *rep = selected->first (); rep; rep = selected->next ())
00150     {
00151         // FIXME: by definition all the items *are* already checked...
00152 
00153         // this will automatically set the visibility, emit the signals
00154         // broadcast a change message and update all other browsers
00155         rep->check (rep->checked ());
00156     }
00157 
00158     // FIXME: broadcast some message that will dump out the info?
00159     // printTrees ();
00160     delete selected;
00161 }
00162 
00164 //  void
00165 //  IgQtTwigBrowser::printTrees (void)
00166 //  {
00167 //      for (List_of_Twigs::iterator i = rootTwigs.begin ();
00168 //           i != rootTwigs.end(); i++)
00169 //          (*i)->printTree();
00170 //  }
00171 #endif // 0
00172 
00174 void
00175 IgQtTwigBrowser::onSelectionChange (void)
00176 {
00177     // FIXME: broadcast a single message for multiple / single selection?
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         // Prevent the item from opening on double click
00214         /* emit */ 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         // Ask the rep to make the change (and commit the change back
00231         // to the object, and broadcast it to all other browsers).
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     // Readjust for horizontally scrolled view.
00257     x += contentsX ();
00258 
00259     // We have two checkable columns: traversal (1) and visibility
00260     // (2).  Check if the position `x' is in either; and return the
00261     // index of that column if so; otherwise return -1.
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     // Pop up the menu and update it in case it was torn off.
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     // LOG(..., "slot item changed" << std::endl);
00360 }
00361 
00362 void
00363 IgQtTwigBrowser::slotItemRMClicked (void)
00364 {
00365     // LOG(..., "slot item right mouse clicked" << std::endl);
00366 }

Generated on Tue Jun 9 17:38:50 2009 for CMSSW by  doxygen 1.5.4