00001
00002 #include "StarGeomTreeWidget.h"
00003 #include "TVolume.h"
00004 #include "TROOT.h"
00005 #include "TPad.h"
00006 #include "TSystem.h"
00007 #include "TContextMenu.h"
00008 #include "TColor.h"
00009 #include "TGeometry.h"
00010 #include "TGeoManager.h"
00011 #include "TVolumePosition.h"
00012 #include "TVolumeView.h"
00013 #include "TDataSetIter.h"
00014
00015 #include "TQtIconBrowserImp.h"
00016 #include "TQMimeTypes.h"
00017
00018 #include "QtGBrowserGeoDrawHelper.h"
00019
00020 #include <map>
00021 #include <stack>
00022 using namespace std;
00023
00024 #include <QAbstractScrollArea>
00025 #include <QFile>
00026 #include <QTextStream>
00027 #include <QMessageBox>
00028 #include <QColorDialog>
00029 #include <QMenu>
00030 #include <QDebug>
00031 #include <QMouseEvent>
00032 #include <QKeyEvent>
00033 #include <QStyledItemDelegate>
00034 #include <QApplication>
00035 #include <QDataStream>
00036 #include <QFile>
00037
00038 class GeomBrowseItemDeledate : public QStyledItemDelegate {
00039 public:
00040
00044 GeomBrowseItemDeledate(QObject *parent): QStyledItemDelegate(parent) {}
00045 ~GeomBrowseItemDeledate(){}
00049 bool editorEvent(QEvent *event,
00050 QAbstractItemModel *model,
00051 const QStyleOptionViewItem &option,
00052 const QModelIndex &index)
00053 {
00054 Q_ASSERT(event);
00055 Q_ASSERT(model);
00056
00057
00058 Qt::ItemFlags flags = model->flags(index);
00059 if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
00060 || !(flags & Qt::ItemIsEnabled))
00061 return false;
00062
00063
00064 QVariant value = index.data(Qt::CheckStateRole);
00065 if (!value.isValid())
00066 return false;
00067 const QWidget *widget = 0;
00068
00069 if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option))
00070 widget = v3->widget;
00071 QStyle *style = widget ? widget->style() : QApplication::style();
00072
00073
00074 if ((event->type() == QEvent::MouseButtonRelease)
00075 || (event->type() == QEvent::MouseButtonDblClick)) {
00076 QStyleOptionViewItemV4 viewOpt(option);
00077 initStyleOption(&viewOpt, index);
00078 QRect checkRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &viewOpt, widget);
00079 QMouseEvent *me = static_cast<QMouseEvent*>(event);
00080 if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
00081 return false;
00082
00083
00084 if (event->type() == QEvent::MouseButtonDblClick)
00085 return true;
00086
00087 } else if (event->type() == QEvent::KeyPress) {
00088 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
00089 && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
00090 return false;
00091 } else {
00092 return false;
00093 }
00094
00095 Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
00096 switch (state) {
00097 case Qt::Checked: state = Qt::Unchecked; break;
00098 case Qt::Unchecked: state = Qt::PartiallyChecked; break;
00099 case Qt::PartiallyChecked:state = Qt::Checked; break;
00100 };
00101 return model->setData(index, state, Qt::CheckStateRole);
00102 }
00103 };
00104
00105
00106
00107
00108
00109
00110
00111 static QIcon GetGeoIcon(const char *className) {
00112 QString cN = QString(className).toLower();
00113 cN.remove(0,1);
00114 cN.replace("brik","bbox");
00115 if (!cN.startsWith("geo")) cN.insert(0,"geo");
00116 cN+="_t.xpm";
00117 TString iconsPath = "$ROOTSYS/icons";
00118 gSystem->ExpandPathName(iconsPath);
00119 QString fullPath = iconsPath.Data();
00120 fullPath += "/"; fullPath += cN;
00121 if (!gSystem->AccessPathName(fullPath.toAscii().data())) {
00122 return QIcon(fullPath);
00123 }
00124 return QIcon();
00125 }
00126
00127 static QTreeWidgetItem *Find(QTreeWidgetItem *from, TObject *topObject)
00128 {
00129
00130 return 0;
00131 }
00132
00133 static pair<QString,QString> MakeVolumeDsc(const QString &s)
00134 {
00135
00136
00137 QString key = s.left(4);
00138 pair<QString, QString> volumeDiscriptor(key,s);
00139 return volumeDiscriptor;
00140 }
00141
00142
00143 static map<QString,QString> MakeVolumeMap(const QString &fileName)
00144 {
00145 QFile file( fileName );
00146 map<QString,QString> thisMap;
00147 if ( file.open( IO_ReadOnly ) ) {
00148 QTextStream stream( &file );
00149 QString line;
00150 while ( !stream.atEnd() ) {
00151 line = stream.readLine();
00152 thisMap.insert(MakeVolumeDsc(line));
00153 }
00154 file.close();
00155 }
00156 return thisMap;
00157 }
00158
00159
00160
00161 static const QString &GetVolumeDescriptor(const QString &volumeName,bool richText=false)
00162 {
00163 static bool first = true;
00164 static map<QString,QString> volumeMap;
00165 static QString dsc;
00166 static bool errorMessage = false;
00167 if (first) {
00168 first = false;
00169 TString helpFile = "volumes.txt";
00170 const char *fullPath = gSystem->Which("./:./StDb/geometry:$STAR/StDb/geometry",helpFile);
00171 if (fullPath) {
00172 volumeMap = MakeVolumeMap(fullPath);
00173 } else if (!errorMessage) {
00174 errorMessage = true;
00175 QMessageBox::critical(0
00176 ,"STAR Geometry narrative description"
00177 ,QString("No file <%1> file under %2 was found")
00178 .arg(helpFile.Data())
00179 .arg("./:./StDb/geometry:$STAR/StDb/geometry"));
00180 }
00181 delete [] fullPath; fullPath=0;
00182 }
00183 map<QString,QString>::iterator it;
00184 it = volumeMap.find(volumeName);
00185 if (it!=volumeMap.end()) {
00186 dsc = richText ? "<p><b>" : "";
00187 dsc += it->second;
00188 } else dsc = "";
00189 dsc.replace(" comment =",richText ? ":</b> ": ": ");
00190 return dsc;
00191 }
00192
00193
00194
00195 StarGeomTreeWidget::StarGeomTreeWidget(QWidget *parent) : QTreeWidget(parent)
00196 , fContextMenu(0),fGeoManager2Delete(0),fCurrentDrawn(0),fNewItemCreating(false)
00197 , fPopupContextMenu(0),fDepth(0)
00198 {
00199 Init();
00200 }
00201
00202
00203 void StarGeomTreeWidget::Init()
00204 {
00205 SetDepthCB(3);
00206 this->setContextMenuPolicy(Qt::CustomContextMenu);
00207 QStringList labels;
00208 labels << "Name" << "Title" << "# <- mother" <<"Class";
00209 this->setHeaderLabels(labels);
00210 this->setColumnCount(4);
00211 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
00212 this->setItemDelegate(new GeomBrowseItemDeledate(this));
00213 ConnectTreeSlots();
00214 setMouseTracking(true);
00215 setUniformRowHeights(true);
00216 }
00217
00218
00219 void StarGeomTreeWidget::ConnectTreeSlots()
00220 {
00221 connect(this,SIGNAL(currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *)),this,SLOT(currentItemChangedCB( QTreeWidgetItem *, QTreeWidgetItem *)));
00222 connect(this,SIGNAL(itemActivated(QTreeWidgetItem *, int)), this,SLOT(itemActivatedCB( QTreeWidgetItem *, int) ));
00223 connect(this,SIGNAL(itemChanged( QTreeWidgetItem *, int)), this,SLOT(itemChangedCB( QTreeWidgetItem *, int)));
00224 connect(this,SIGNAL(itemClicked( QTreeWidgetItem *, int )), this,SLOT(itemClickedCB( QTreeWidgetItem *, int )));
00225 connect(this,SIGNAL(itemCollapsed( QTreeWidgetItem *)), this,SLOT(itemCollapsedCB( QTreeWidgetItem *)));
00226 connect(this,SIGNAL(itemDoubleClicked( QTreeWidgetItem * , int )), this,SLOT(itemDoubleClickedCB( QTreeWidgetItem * , int )));
00227 connect(this,SIGNAL(itemEntered( QTreeWidgetItem *, int)), this,SLOT(itemEnteredCB( QTreeWidgetItem *, int)));
00228 connect(this,SIGNAL(itemExpanded( QTreeWidgetItem * )), this,SLOT(itemExpandedCB( QTreeWidgetItem * )));
00229 connect(this,SIGNAL(itemPressed( QTreeWidgetItem * , int )), this,SLOT(itemPressedCB( QTreeWidgetItem * , int)));
00230 connect(this,SIGNAL(itemSelectionChanged()), this,SLOT(itemSelectionChangedCB()));
00231 connect(this,SIGNAL(customContextMenuRequested(const QPoint & )), this,SLOT(contextMenuRequestedCB(const QPoint & )));
00232 }
00233
00234 static void unRollChildren( QDataStream & stream, QTreeWidgetItem &top, bool out = true)
00235 {
00236 if (out) stream << top;
00237 else stream >> top;
00238 int n = top.childCount ();
00239 for (int i=0; i<n; ++i) {
00240 QTreeWidgetItem *item = top.child(i);
00241 unRollChildren(stream, *item, out);
00242 }
00243 }
00244
00245 QDataStream & operator>> ( QDataStream & in, StarGeomTreeWidget & item )
00246 {
00247 return in;
00248 }
00249
00250 QDataStream & operator<< ( QDataStream & out, const StarGeomTreeWidget & item )
00251 {
00252 int nItem = item.topLevelItemCount();
00253 for (int i=0; i<nItem; ++i) {
00254 QTreeWidgetItem *it = item.topLevelItem (i);
00255 unRollChildren(out,*it);
00256 }
00257 return out;
00258 }
00259
00260 StarGeomTreeWidget::~StarGeomTreeWidget()
00261 {
00262 #ifdef QT_IO_4_STAR
00263 {
00264 QFile file("file.dat");
00265 file.open(QIODevice::WriteOnly);
00266 QDataStream out(&file);
00267 out << this;
00268 }
00269 #endif
00270
00271 delete itemDelegate();
00272 }
00273
00274
00275 QTreeWidgetItem *StarGeomTreeWidget::CreateTreeWidgetItem(TVolume *volume, QTreeWidgetItem *parent,const QString &nConter)
00276 {
00277
00278
00279 QStringList strings;
00280 strings.append(volume->GetName());
00281 strings.append(volume->GetTitle());
00282 strings.append(nConter);
00283 strings.append(volume->ClassName());
00284 fNewItemCreating = true;
00285 QTreeWidgetItem* item = parent ?
00286 new QTreeWidgetItem(parent,strings)
00287 :
00288 new QTreeWidgetItem(this,strings);
00289 item->setData(0,Qt::UserRole,qVariantFromValue((void *)volume));
00290 item->setChildIndicatorPolicy(
00291 volume->GetListSize() >0 ? QTreeWidgetItem::ShowIndicator
00292 : QTreeWidgetItem::DontShowIndicatorWhenChildless );
00293 SetVisibility(item, volume->GetVisibility());
00294 if (QString(volume->GetName()).startsWith("{")) {
00295
00296 for (int i=0;i<4;i++) item->setData(i,Qt::ForegroundRole,Qt::blue);
00297 }
00298
00299 stack<QString> fullPath;
00300 fullPath.push(item->text(0));
00301 QTreeWidgetItem *itemParent = item;
00302 while (itemParent && (itemParent = itemParent->parent()) ) {
00303 fullPath.push("/");
00304 fullPath.push(itemParent->text(0));
00305 }
00306
00307 QString tip;
00308 while (!fullPath.empty()) {
00309 tip += fullPath.top();
00310 fullPath.pop();
00311 if (fullPath.size() == 2) item->setToolTip(2,tip);
00312 }
00313 item->setToolTip(0,tip);
00314
00315 TShape *sh = volume->GetShape();
00316 if (sh) {
00317 const QIcon *set = 0;
00318 QIcon geoIcon;
00319 const char *shapeClassName = sh->ClassName();
00320 set = TQtIconBrowserImp::Shape2GeoShapeIcon(shapeClassName);
00321 if (!set) {
00322 geoIcon = GetGeoIcon(shapeClassName);
00323 set = &geoIcon;
00324 }
00325 item->setIcon(0,*set);
00326 item->setIcon(2,*set);
00327 }
00328 fNewItemCreating = false;
00329 return item;
00330 }
00331
00332
00333 void StarGeomTreeWidget::currentItemChangedCB ( QTreeWidgetItem * current, QTreeWidgetItem * previous ) {}
00334
00335 void StarGeomTreeWidget::itemActivatedCB ( QTreeWidgetItem * item, int column )
00336 {}
00337
00338 void StarGeomTreeWidget::itemChangedCB ( QTreeWidgetItem * item, int)
00339 {
00340 if (item) {
00341 #if 0
00342
00343
00344 if (!fNewItemCreating) {
00345 TObject *obj = CurrentObject(item);
00346 TVolume *volume = dynamic_cast<TVolume *>(obj);
00347 TVolume::ENodeSEEN vis = volume->GetVisibility();
00348 if (vis == TVolume::kNoneVisible)
00349 vis = TVolume::kThisUnvisible;
00350 else if (vis == TVolume::kThisUnvisible)
00351 vis = TVolume::kBothVisible;
00352 else
00353 vis = TVolume::kNoneVisible;
00354 SetVisibility(item, vis);
00355 }
00356 #endif
00357 }
00358 }
00359
00360
00361 void StarGeomTreeWidget::itemClickedCB ( QTreeWidgetItem * item, int column )
00362 {
00363 if (item) {
00364
00365 TObject *obj = CurrentObject(item);
00366 TVolume *volume = dynamic_cast<TVolume *>(obj);
00367
00368
00369 if (volume) {
00370 TVolume::ENodeSEEN s = TVolume::kThisUnvisible;
00371 switch (item->checkState (0)) {
00372 case Qt::Unchecked: s = TVolume::kNoneVisible; break;
00373 case Qt::PartiallyChecked: s = TVolume::kThisUnvisible; break;
00374 case Qt::Checked: s = TVolume::kBothVisible; break;
00375 default: s = TVolume::kThisUnvisible; break;
00376 };
00377
00378 if (volume->GetVisibility() != s) {
00379 volume->SetVisibility(s);
00380
00381 QString m = "The ";
00382 if ( s & TVolume::kThisUnvisible) m += "in";
00383 m += "visible volume: ";
00384 const char *info = obj->GetObjectInfo(gPad->XtoPixel(0),gPad->YtoPixel(0));
00385 if (info) m += info;
00386 emit ObjectInfo(m);
00387
00388
00389 if (item == this->currentItem() ) drawItem(item, false);
00390
00391
00392 if (fCurrentDrawn == item) drawItem(item, true);
00393 else {
00394
00395 QTreeWidgetItem *lookup = item;
00396 int depth = fDepth;
00397 while( lookup && depth && (lookup != fCurrentDrawn)) {
00398 lookup = lookup->parent();
00399 depth--;
00400 }
00401 if (lookup && depth) drawItem(fCurrentDrawn, true);
00402 }
00403 }
00404 #if 0
00405 QString dsc = GetVolumeDescriptor(volume->GetName(),true);
00406 if (dsc.isEmpty()) {
00407 dsc = QString("<p>No desciption was found for <b>%1</b><br>Edit the <code>volumes.txt</code> file")
00408 .arg(volume->GetName());
00409 }
00410
00411 #endif
00412 }
00413 }
00414 }
00415
00416
00417 void StarGeomTreeWidget::itemCollapsedCB ( QTreeWidgetItem * item )
00418 {
00419 if (item) {
00420
00421 TQtLockUpdateWidget listLock(this);
00422
00423 QList<QTreeWidgetItem *> children = item->takeChildren();
00424 for (int i = 0; i< children.size(); i++) {
00425 if (fCurrentDrawn == children.at(i)) {fCurrentDrawn = 0; break;}
00426 }
00427
00428 while(!children.isEmpty()) delete(children.takeLast());
00429 }
00430 }
00431
00432
00433 void StarGeomTreeWidget::itemDoubleClickedCB ( QTreeWidgetItem * item, int column )
00434 {
00435 if (item && fCurrentDrawn != item) {
00436
00437 {
00438 TQtLockUpdateWidget listLock(this);
00439
00440 TObject *obj = 0;
00441 const QIcon *set = 0;
00442 QIcon geoIcon;
00443 const char *shapeClassName = 0;
00444 if ( fCurrentDrawn ) {
00445 obj = CurrentObject();
00446 if (obj) {
00447 if (obj->InheritsFrom(TVolume::Class()) ) {
00448 TShape *sh = ((TVolume *)obj)->GetShape();
00449 if (sh) {
00450 shapeClassName = sh->ClassName();
00451 set = TQtIconBrowserImp::Shape2GeoShapeIcon(shapeClassName);
00452 }
00453 } else if (obj->InheritsFrom(TVolumeView::Class()) ) {
00454 shapeClassName = ((TVolumeView *)obj)->GetShape()->ClassName();
00455 set = TQtIconBrowserImp::Shape2GeoShapeIcon(shapeClassName);
00456 } else if (obj->InheritsFrom(TShape::Class()) ) {
00457 set = TQtIconBrowserImp::Shape2GeoShapeIcon(((TShape *)obj)->ClassName());
00458 } else if (obj->InheritsFrom(TGeoVolume::Class()) )
00459 set = TQtIconBrowserImp::IconList()->GetIcon(((TGeoVolume *)(obj))->GetShape()->GetName());
00460 }
00461 if (!set) {
00462 geoIcon = GetGeoIcon(shapeClassName);
00463 set = &geoIcon;
00464 }
00465 fCurrentDrawn->setIcon(0,set ? *set: QIcon());
00466 fCurrentDrawn->setIcon(1,QIcon());
00467 for (int i=0;i<4;i++) fCurrentDrawn->setData(i,Qt::ForegroundRole,Qt::black);
00468 }
00469
00470 fCurrentDrawn = item;
00471 obj = CurrentObject();
00472 if (obj) {
00473 if (obj->InheritsFrom(TVolume::Class()) ) {
00474 TShape *sh = ((TVolume *)obj)->GetShape();
00475 if (sh) {
00476 shapeClassName = sh->ClassName();
00477 set = TQtIconBrowserImp::Shape2GeoShapeIcon(shapeClassName);
00478 }
00479 } else if (obj->InheritsFrom(TVolumeView::Class()) )
00480 set = TQtIconBrowserImp::Shape2GeoShapeIcon(((TVolumeView *)obj)->GetShape()->ClassName());
00481 else if (obj->InheritsFrom(TShape::Class()) )
00482 set = TQtIconBrowserImp::Shape2GeoShapeIcon(((TShape *)obj)->ClassName());
00483 else if (obj->InheritsFrom(TGeoVolume::Class()) )
00484 set = TQtIconBrowserImp::IconList()->GetIcon(((TGeoVolume *)(obj))->GetShape()->GetName());
00485 }
00486
00487 if (!set) {
00488 geoIcon = GetGeoIcon(shapeClassName);
00489 set = &geoIcon;
00490 }
00491 fCurrentDrawn->setIcon(0,QIcon(":/wirebox.xpm"));
00492
00493 fCurrentDrawn->setIcon(1,set ? *set : QIcon(":/arrow_left.xpm"));
00494 for (int i=0;i<4;i++) fCurrentDrawn->setData(i,Qt::ForegroundRole,Qt::red);
00495 }
00496 drawItem(fCurrentDrawn, true);
00497 }
00498 }
00499
00500
00501 void StarGeomTreeWidget::itemEnteredCB ( QTreeWidgetItem * item, int column )
00502 {
00503 if (item) {
00504 TObject *obj = CurrentObject(item);
00505 if (obj && gPad) {
00506 QString m = "The ";
00507 TVolume::ENodeSEEN s = ((TVolume *)obj)->GetVisibility();
00508 if ( s & TVolume::kThisUnvisible) m += "in";
00509 m += "visible volume: ";
00510 QString dsc= GetVolumeDescriptor(obj->GetName());
00511 if (dsc.isEmpty() ) {
00512 const char *info = obj->GetObjectInfo(gPad->XtoPixel(0),gPad->YtoPixel(0));
00513 if (info) m += info;
00514 } else {
00515 m += dsc;
00516 }
00517 emit ObjectInfo(m);
00518 }
00519 }
00520 }
00521
00522
00523 inline static Int_t CountInstances(TVolume *parent, TVolume *child)
00524 {
00525
00526 Int_t counter = 0;
00527 TList *positions = parent->GetListOfPositions();
00528 TIter next(positions);
00529 while (TVolumePosition *pos = (TVolumePosition *)next() )
00530 if (pos->GetNode() == child ) counter++;
00531 return counter;
00532 }
00533
00534
00535
00536 void StarGeomTreeWidget::itemExpandedCB ( QTreeWidgetItem * item )
00537 {
00538 if (item) {
00539
00540 TObject *obj = CurrentObject(item);
00541 TVolume *volume = dynamic_cast<TVolume *>(obj);
00542
00543 TQtLockUpdateWidget listLock(this);
00544 this->setSortingEnabled(false);
00545
00546 TDataSetIter next(volume);
00547 TVolume *child = 0;
00548 QString volName = volume->GetName();
00549 while ( (child = (TVolume *)next()) ) {
00550 Int_t nVolume = CountInstances(volume,child);
00551 CreateTreeWidgetItem(child,item, (nVolume >1) ? QString("#%1 <- %2").arg(nVolume).arg(volName) : QString());
00552 }
00553 this->setSortingEnabled(true);
00554 int currentWidth = columnWidth(0);
00555 this->resizeColumnToContents(0);
00556 if (currentWidth > this->columnWidth(0) ) {
00557
00558 this->setColumnWidth(0,currentWidth);
00559 }
00560 }
00561 }
00562
00563 void StarGeomTreeWidget::itemPressedCB ( QTreeWidgetItem * item, int column ) {}
00564
00565 void StarGeomTreeWidget::itemSelectionChangedCB ()
00566 {
00567 QTreeWidgetItem *i = this->currentItem();
00568 if (i && i->isSelected() ) TreeView_selectionChanged(i);
00569 }
00570
00571
00572 void StarGeomTreeWidget::TreeView_selectionChanged(QTreeWidgetItem *item)
00573 {
00574 int selectedItems = this->selectedItems().size();
00575 if (item && (selectedItems == 1)) drawItem(item,false);
00576 }
00577
00578
00579 void StarGeomTreeWidget::drawItem( QTreeWidgetItem *item, bool expanded)
00580 {
00581 if (item) {
00582 TObject *rootObject = CurrentObject(item);
00583 emit DrawObject(rootObject,expanded);
00584 }
00585 }
00586
00587
00588 void StarGeomTreeWidget::contextMenuRequestedCB(const QPoint &pos)
00589 {
00590 QList<QTreeWidgetItem *>lst = this->selectedItems();
00591
00592 int nSelected = lst.size();
00593 if (nSelected == 1) {
00594 if (!fContextMenu) fContextMenu = new TContextMenu("BrowserContextMenu");
00595 QTreeWidgetItem *that = lst.first();
00596 QVariant model = that->data(0, Qt::UserRole);
00597 TObject *obj = (TObject *)model.value<void *>();
00598 if (obj)
00599 fContextMenu->Popup(QCursor::pos().x(),QCursor::pos().y(), obj,(TBrowser *)0);
00600 } else {
00601 QAction *response = 0;
00602 static std::vector<QAction *> menus;
00603 if (!fPopupContextMenu) {
00604 menus.clear();
00605 fPopupContextMenu = new QMenu(this);
00606 fPopupContextMenu->setTitle("Visibility:");
00607 QAction *action = fPopupContextMenu->menuAction();
00608 QFont af = action->font();
00609 af.setBold(true);
00610 action->setFont(af);
00611 QAction *itemPosition = 0;
00612
00613 itemPosition = fPopupContextMenu->addAction("&Both");
00614 itemPosition->setWhatsThis("Make the selected volumes and its children visible");
00615 menus.push_back(itemPosition);
00616
00617 itemPosition=fPopupContextMenu->addAction("&Children");
00618 itemPosition->setWhatsThis("Make the selected children of the selected volumes visible but the volume itself none");
00619 menus.push_back(itemPosition);
00620
00621 itemPosition=fPopupContextMenu->addAction("&None");
00622 itemPosition->setWhatsThis("Make the selected volumes invisible");
00623 menus.push_back(itemPosition);
00624
00625 fPopupContextMenu->addSeparator();
00626
00627 itemPosition=fPopupContextMenu->addAction("&Save");
00628 itemPosition->setWhatsThis("Save the selected object into ROOT file");
00629 menus.push_back(itemPosition);
00630
00631 itemPosition=fPopupContextMenu->addAction("&Color");
00632 itemPosition->setWhatsThis("Change the color of the selected object");
00633 menus.push_back(itemPosition);
00634 itemPosition=fPopupContextMenu->addAction("&Wired");
00635 itemPosition->setWhatsThis("Make the selected object \"wired\"");
00636 menus.push_back(itemPosition);
00637 }
00638 response = fPopupContextMenu->exec(QCursor::pos());
00639 if (response) {
00640 TQtLockUpdateWidget listLock(this);
00641
00642 Color_t rootColor = -1;
00643 Style_t rootStyle = -1;
00644
00645
00646 for (int i = 0; i < nSelected; ++i) {
00647 QTreeWidgetItem* itemRoot =lst.at(i);
00648 QVariant model = itemRoot->data(0, Qt::UserRole);
00649 TObject *obj = (TObject *)model.value<void *>();
00650 TVolume *volume = dynamic_cast<TVolume *>(obj);
00651
00652
00653 if (volume) {
00654 TVolume::ENodeSEEN s = volume->GetVisibility();
00655 if ( response == menus[0] ) {
00656 itemRoot->setCheckState(0,Qt::Checked); s = TVolume::kBothVisible;
00657 } else if (response == menus[1]) {
00658 itemRoot->setCheckState(0,Qt::PartiallyChecked); s = TVolume::kThisUnvisible;
00659 } else if (response == menus[2]) {
00660 itemRoot->setCheckState(0,Qt::Unchecked) ; s = TVolume::kNoneVisible;
00661 } else if (response == menus[3]) {
00662 #if 0
00663 if (topVolumeToSave) {
00664 topVolumeToSave->Add(volume);
00665 } else if (!saved) {
00666 saved = true;
00667
00668 QString filter = "ROOT file (*.root);";
00669 QString selectedFilter;
00670 QString dir = fSaveFileName;
00671 if (dir.isEmpty()) dir = gSystem->WorkingDirectory();
00672 else dir = QFileInfo(dir).dirPath();
00673
00674 QString thatFile = QFileDialog::getSaveFileName(dir
00675 , filter, this, "SaveAs"
00676 , "Save the volulme as"
00677 , &selectedFilter);
00678
00679 if (thatFile.isEmpty()) {
00680 response = 0;
00681 } else {
00682 TDirectory *save = gDirectory;
00683 file2Save = TFile::Open((const char *)thatFile,"RECREATE");
00684 topVolumeToSave = new TVolume("GeomBrowse","saved",(TShape *)0);
00685 save->cd();
00686 }
00687 }
00688 #endif
00689 } else if (response == menus[4]) {
00690
00691 Color_t vc = volume->GetLineColor();
00692 Style_t vs = volume->GetFillStyle();
00693 if (rootColor == -1) {
00694 float r,g,b;
00695 int a = 0;
00696 gROOT->GetColor(vc)->GetRGB(r,g,b);
00697 if (4000 >= vs && vs < 5000) a = vs-4000;
00698 QRgb initial = qRgba(int(r*255),int(g*255),int(b*255),a);
00699 bool ok;
00700 QRgb color = QColorDialog::getRgba(initial, &ok, this);
00701 if (ok) {
00702 int red = qRed(color);
00703 int green = qGreen(color);
00704 int blue = qBlue(color);
00705 int alpha = qAlpha(color);
00706 rootColor = TColor::GetColor(red, green, blue);
00707 if ( (alpha > 0) && (alpha != vs-4000) ) rootStyle = 4000+alpha;
00708 } else {
00709 response = 0;
00710 }
00711 }
00712 if (rootColor != -1 && vc != rootColor) volume->SetLineColor(rootColor);
00713 if (rootStyle != -1 && vs != rootStyle) volume->SetFillStyle(rootStyle);
00714 } else if (response == menus[5]) {
00715 volume->SetFillStyle(4001);
00716 } else { response = 0; }
00717
00718 if (volume->GetVisibility() != s) volume->SetVisibility(s);
00719 }
00720 }
00721 #if 0
00722 if (file2Save) {
00723 TDirectory *save = gDirectory;
00724 file2Save->cd();
00725 topVolumeToSave->Write();
00726 file2Save->Close();
00727 delete file2Save; file2Save=0;
00728 delete topVolumeToSave; topVolumeToSave = 0;
00729 save->cd();
00730 }
00731 #endif
00732 }
00733
00734 }
00735 }
00736
00737
00738 void StarGeomTreeWidget::SetVisibility( QTreeWidgetItem * item, TVolume::ENodeSEEN vis )
00739 {
00740
00741 if (item) {
00742 blockSignals(true);
00743 switch (vis) {
00744 case TVolume::kBothVisible: item->setCheckState(0,Qt::Checked); break;
00745 case TVolume::kSonUnvisible: item->setCheckState(0,Qt::Checked); break;
00746 case TVolume::kThisUnvisible: item->setCheckState(0,Qt::PartiallyChecked);break;
00747 case TVolume::kNoneVisible: item->setCheckState(0,Qt::Unchecked); break;
00748 };
00749 blockSignals(false);
00750 }
00751 }
00752
00753 QTreeWidgetItem* StarGeomTreeWidget::AddModel2ListView( TObject *obj, const QString &title)
00754 {
00755 TVolume *volume = 0;
00756 TQtLockUpdateWidget listLock(this);
00757 this->setSortingEnabled(false);
00758 QTreeWidgetItem* item = 0;
00759 if ( obj->IsA() == TGeometry::Class()) {
00760 volume = new TVolume(*((TGeometry*)obj)->GetCurrentNode());
00761 } else if (obj->IsA() == TGeoManager::Class()) {
00762 volume = TGeoDrawHelper::MakeVolume(((TGeoManager*)obj)->GetTopVolume());
00763 fGeoManager2Delete = (TGeoManager*)obj;
00764 } else if (obj->IsA() == TGeoVolume::Class()) {
00765 volume = TGeoDrawHelper::MakeVolume((TGeoVolume*)obj);
00766 } else if (obj->IsA() == TVolume::Class()) {
00767 volume = (TVolume *)obj;
00768 }
00769 if(volume) {
00770 item = CreateTreeWidgetItem(volume);
00771 this->addTopLevelItem(item);
00772 }
00773 if (!title.isEmpty() && item) item->setText(0,title);
00774 this->setSortingEnabled(true);
00775 return item;
00776 }
00777
00778
00779 void StarGeomTreeWidget::SelectByTObject( TObject *obj, const QPoint &)
00780 {
00781
00782 if ( fCurrentDrawn && obj->InheritsFrom(TDataSet::Class()) ) {
00783
00784
00785 TQtLockUpdateWidget listLock(this);
00786
00787 QVariant model = fCurrentDrawn->data(0, Qt::UserRole);
00788 TObject *topObject = (TObject *)model.value<void *>();
00789
00790 stack<TObject *> items;
00791 items.push(obj);
00792 TDataSet *set = (TDataSet *)obj;
00793 while ( (set = set->GetParent()) ) {
00794 items.push(set);
00795
00796 if (((TObject *)set) == topObject) break;
00797 }
00798 if (set) {
00799
00800
00801 QTreeWidgetItem *selectedItem = fCurrentDrawn;
00802 while ( !items.empty() ) {
00803 topObject = items.top(); items.pop();
00804 QTreeWidgetItem *nextSelectedItem = Find(selectedItem,topObject);
00805
00806 if (nextSelectedItem) {
00807 #ifdef TOBE_DONE_YET
00808 if ( !nextSelectedItem->isOpen() ) ((TQtObjectListItem *)nextSelectedItem)->setOpen(true);
00809 #endif
00810 selectedItem = nextSelectedItem;
00811 }
00812 }
00813 this->setCurrentItem(selectedItem,0,QItemSelectionModel::SelectCurrent);
00814 this->scrollToItem(selectedItem);
00815 }
00816 }
00817 }
00818
00819
00820 void StarGeomTreeWidget::ClearCB()
00821 {
00822
00823 int nItem = this->topLevelItemCount();
00824 for (;nItem;nItem--) {
00825 QTreeWidgetItem * item = this->topLevelItem (nItem-1);
00826 delete CurrentObject(item);
00827 }
00828 this->clear();
00829 }
00830
00831
00832 TObject * StarGeomTreeWidget::CurrentObject(QTreeWidgetItem *item)
00833 {
00834
00835 if (!item) item = fCurrentDrawn;
00836 QVariant model = item->data(0, Qt::UserRole);
00837 TObject *obj = (TObject *)model.value<void *>();
00838 return obj;
00839 }
00840
00841
00842 void StarGeomTreeWidget::SetDepthCB(int depth)
00843 {
00844 fDepth = depth;
00845 }