Debian Codegraph @

src/Gui/TreeView.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <QMouseEvent>
25
26
27 #include "TreeView.h"
28 #include "Application.h"
29 #include "Document.h"
30 #include "DocumentModel.h"
31 #include "MainWindow.h"
32 #include "MDIView.h"
33 #include "ViewProvider.h"
34
35 using namespace Gui;
36
37 TreeView::TreeView(QWidget* parent)
38 : QTreeView(parent)
39 {
40 setModel(new DocumentModel(this));
41 QModelIndex root = this->model()->index(0, 0, QModelIndex());
42 this->setExpanded(root, true);
43 this->setDragEnabled(true);
44 this->setAcceptDrops(true);
45 this->setDropIndicatorShown(false);
46 this->setRootIsDecorated(false);
47 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
48 this->setMouseTracking(true); // needed for itemEntered() to work
49 }
50
51 TreeView::~TreeView() = default;
52
53 void TreeView::mouseDoubleClickEvent(QMouseEvent* event)
54 {
55 QModelIndex index = indexAt(event->pos());
56 if (!index.isValid() || index.internalPointer() == Application::Instance) {
57 return;
58 }
59 Base::BaseClass* item = nullptr;
60 item = static_cast<Base::BaseClass*>(index.internalPointer());
61 if (item->is<Document>()) {
62 QTreeView::mouseDoubleClickEvent(event);
63 const Gui::Document* doc = static_cast<Gui::Document*>(item);
64 MDIView* view = doc->getActiveView();
65 if (!view) {
66 return;
67 }
68 getMainWindow()->setActiveWindow(view);
69 }
70 else if (item->isDerivedFrom<ViewProvider>()) {
71 if (!static_cast<ViewProvider*>(item)->doubleClicked()) {
72 QTreeView::mouseDoubleClickEvent(event);
73 }
74 }
75 }
76
77 void TreeView::rowsInserted(const QModelIndex& parent, int start, int end)
78 {
79 QTreeView::rowsInserted(parent, start, end);
80 if (parent.isValid()) {
81 auto ptr = static_cast<Base::BaseClass*>(parent.internalPointer());
82 // type is defined in DocumentModel.cpp
83 if (ptr->getTypeId() == Base::Type::fromName("Gui::ApplicationIndex")) {
84 for (int i = start; i <= end; i++) {
85 QModelIndex document = this->model()->index(i, 0, parent);
86 this->expand(document);
87 }
88 }
89 }
90 }
91
92 #include "moc_TreeView.cpp"