FreeCAD

Форк
0
/
DocumentModel.cpp 
678 строк · 23.6 Кб
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
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <QApplication>
27
# include <QFont>
28
#endif
29

30
#include <App/Document.h>
31
#include <App/DocumentObject.h>
32
#include <App/PropertyLinks.h>
33

34
#include "DocumentModel.h"
35
#include "Application.h"
36
#include "BitmapFactory.h"
37
#include "Document.h"
38
#include "ViewProviderDocumentObject.h"
39

40

41
using namespace Gui;
42
namespace sp = std::placeholders;
43

44
namespace Gui {
45
    // forward declaration
46
    class ViewProviderIndex;
47

48
    // Base class
49
    class DocumentModelIndex : public Base::BaseClass
50
    {
51
        TYPESYSTEM_HEADER_WITH_OVERRIDE();
52

53
    public:
54
        ~DocumentModelIndex() override
55
        { qDeleteAll(childItems); }
56

57
        void setParent(DocumentModelIndex* parent)
58
        { parentItem = parent; }
59
        DocumentModelIndex *parent() const
60
        { return parentItem; }
61
        void appendChild(DocumentModelIndex *child)
62
        { childItems.append(child); child->setParent(this); }
63
        void removeChild(int row)
64
        { childItems.removeAt(row); }
65
        QList<DocumentModelIndex*> removeAll()
66
        {
67
            QList<DocumentModelIndex*> list = childItems;
68
            childItems.clear();
69
            return list;
70
        }
71
        DocumentModelIndex *child(int row)
72
        { return childItems.value(row); }
73
        int row() const
74
        {
75
            if (parentItem)
76
                return parentItem->childItems.indexOf
77
                    (const_cast<DocumentModelIndex*>(this));
78
            return 0;
79
        }
80
        int childCount() const
81
        { return childItems.count(); }
82
        virtual QVariant data(int role) const
83
        {
84
            Q_UNUSED(role);
85
            return {};
86
        }
87
        virtual bool setData (const QVariant & value, int role)
88
        {
89
            Q_UNUSED(value);
90
            if (role == Qt::EditRole) {
91
                return true;
92
            }
93

94
            return true;
95
        }
96
        virtual Qt::ItemFlags flags() const
97
        {
98
            return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
99
        }
100

101
    protected:
102
        void reset()
103
        { qDeleteAll(childItems); childItems.clear(); }
104

105
    protected:
106
        DocumentModelIndex() = default;
107
        DocumentModelIndex *parentItem{nullptr};
108
        QList<DocumentModelIndex*> childItems;
109
    };
110

111
    // ------------------------------------------------------------------------
112

113
    // Root node
114
    class ApplicationIndex : public DocumentModelIndex
115
    {
116
        TYPESYSTEM_HEADER_WITH_OVERRIDE();
117

118
    public:
119
        ApplicationIndex() = default;
120
        int findChild(const Gui::Document& d) const;
121
        Qt::ItemFlags flags() const override;
122
        QVariant data(int role) const override;
123
    };
124

125
    // ------------------------------------------------------------------------
126

127
    // Document nodes
128
    class DocumentIndex : public DocumentModelIndex
129
    {
130
        friend class ViewProviderIndex;
131
        TYPESYSTEM_HEADER_WITH_OVERRIDE();
132
        static QIcon* documentIcon;
133
        using IndexSet = boost::unordered_set<ViewProviderIndex*>;
134
        std::map<const ViewProviderDocumentObject*, IndexSet> vp_nodes;
135
        void addToDocument(ViewProviderIndex*);
136
        void removeFromDocument(ViewProviderIndex*);
137

138
    public:
139
        const Gui::Document& d;
140
        explicit DocumentIndex(const Gui::Document& d) : d(d)
141
        {
142
            if (!documentIcon)
143
                documentIcon = new QIcon(Gui::BitmapFactory().pixmap("Document"));
144
        }
145
        ~DocumentIndex() override
146
        {
147
            qDeleteAll(childItems); childItems.clear();
148
        }
149
        ViewProviderIndex* cloneViewProvider(const ViewProviderDocumentObject&) const;
150
        int rowOfViewProvider(const ViewProviderDocumentObject&) const;
151
        void findViewProviders(const ViewProviderDocumentObject&, QList<ViewProviderIndex*>&) const;
152
        QVariant data(int role) const override;
153
    };
154

155
    // ------------------------------------------------------------------------
156

157
    // Object nodes
158
    class ViewProviderIndex : public DocumentModelIndex
159
    {
160
        TYPESYSTEM_HEADER_WITH_OVERRIDE();
161

162
    public:
163
        const Gui::ViewProviderDocumentObject& v;
164
        ViewProviderIndex(const Gui::ViewProviderDocumentObject& v, DocumentIndex* d);
165
        ~ViewProviderIndex() override;
166
        ViewProviderIndex* clone() const;
167
        void findViewProviders(const ViewProviderDocumentObject&, QList<ViewProviderIndex*>&) const;
168
        QVariant data(int role) const override;
169

170
    private:
171
        DocumentIndex* d;
172
    };
173

174
    // ------------------------------------------------------------------------
175

176
    int ApplicationIndex::findChild(const Gui::Document& d) const
177
    {
178
        int child=0;
179
        QList<DocumentModelIndex*>::const_iterator it;
180
        for (it = childItems.begin(); it != childItems.end(); ++it, ++child) {
181
            auto doc = static_cast<DocumentIndex*>(*it);
182
            if (&doc->d == &d)
183
                return child;
184
        }
185

186
        return -1;
187
    }
188

189
    Qt::ItemFlags ApplicationIndex::flags() const
190
    {
191
        return Qt::ItemIsEnabled;
192
    }
193

194
    QVariant ApplicationIndex::data(int role) const
195
    {
196
        if (role == Qt::DecorationRole) {
197
            return qApp->windowIcon();
198
        }
199
        else if (role == Qt::DisplayRole) {
200
            return DocumentModel::tr("Application");
201
        }
202
        return {};
203
    }
204

205
    // ------------------------------------------------------------------------
206

207
    QIcon* DocumentIndex::documentIcon = nullptr;
208

209
    void DocumentIndex::addToDocument(ViewProviderIndex* vp)
210
    {
211
        vp_nodes[&vp->v].insert(vp);
212
    }
213

214
    void DocumentIndex::removeFromDocument(ViewProviderIndex* vp)
215
    {
216
        vp_nodes[&vp->v].erase(vp);
217
    }
218

219
    ViewProviderIndex*
220
    DocumentIndex::cloneViewProvider(const ViewProviderDocumentObject& vp) const
221
    {
222
        std::map<const ViewProviderDocumentObject*, boost::unordered_set<ViewProviderIndex*> >::const_iterator it;
223
        it = vp_nodes.find(&vp);
224
        if (it != vp_nodes.end()) {
225
            boost::unordered_set<ViewProviderIndex*>::const_iterator v;
226
            if (!it->second.empty()) {
227
                v = it->second.begin();
228
                if (*v)
229
                    return (*v)->clone();
230
            }
231
        }
232
        return new ViewProviderIndex(vp, const_cast<DocumentIndex*>(this));
233
    }
234

235
    void DocumentIndex::findViewProviders(const ViewProviderDocumentObject& vp,
236
        QList<ViewProviderIndex*>& index) const
237
    {
238
        QList<DocumentModelIndex*>::const_iterator it;
239
        for (it = childItems.begin(); it != childItems.end(); ++it) {
240
            auto v = static_cast<ViewProviderIndex*>(*it);
241
            v->findViewProviders(vp, index);
242
        }
243
    }
244

245
    int DocumentIndex::rowOfViewProvider(const ViewProviderDocumentObject& vp) const
246
    {
247
        QList<DocumentModelIndex*>::const_iterator it;
248
        int index=0;
249
        for (it = childItems.begin(); it != childItems.end(); ++it, ++index) {
250
            auto v = static_cast<ViewProviderIndex*>(*it);
251
            if (&v->v == &vp)
252
                return index;
253
        }
254

255
        return -1;
256
    }
257

258
    QVariant DocumentIndex::data(int role) const
259
    {
260
        if (role == Qt::DecorationRole) {
261
            return *documentIcon;
262
        }
263
        else if (role == Qt::DisplayRole) {
264
            App::Document* doc = d.getDocument();
265
            return QString::fromUtf8(doc->Label.getValue());
266
        }
267
        else if (role == Qt::FontRole) {
268
            Document* doc = Application::Instance->activeDocument();
269
            QFont font;
270
            font.setBold(doc==&d);
271
            return static_cast<QVariant>(font);
272
        }
273

274
        return {};
275
    }
276

277
    // ------------------------------------------------------------------------
278

279
    ViewProviderIndex::ViewProviderIndex(const Gui::ViewProviderDocumentObject& v, DocumentIndex* d)
280
        : v(v),d(d)
281
    {
282
        if (d) d->addToDocument(this);
283
    }
284

285
    ViewProviderIndex::~ViewProviderIndex()
286
    {
287
        if (d) d->removeFromDocument(this);
288
    }
289

290
    ViewProviderIndex* ViewProviderIndex::clone() const
291
    {
292
        auto copy = new ViewProviderIndex(this->v, this->d);
293
        for (const auto & childItem : childItems) {
294
            ViewProviderIndex* c = static_cast<ViewProviderIndex*>(childItem)->clone();
295
            copy->appendChild(c);
296
        }
297
        return copy;
298
    }
299

300
    void ViewProviderIndex::findViewProviders(const ViewProviderDocumentObject& vp,
301
                                              QList<ViewProviderIndex*>& index) const
302
    {
303
        if (&this->v == &vp)
304
            index.push_back(const_cast<ViewProviderIndex*>(this));
305
        QList<DocumentModelIndex*>::const_iterator it;
306
        for (it = childItems.begin(); it != childItems.end(); ++it) {
307
            auto v = static_cast<ViewProviderIndex*>(*it);
308
            v->findViewProviders(vp, index);
309
        }
310
    }
311

312
    QVariant ViewProviderIndex::data(int role) const
313
    {
314
        if (role == Qt::DecorationRole) {
315
            return v.getIcon();
316
        }
317
        else if (role == Qt::DisplayRole) {
318
            App::DocumentObject* obj = v.getObject();
319
            return QString::fromUtf8(obj->Label.getValue());
320
        }
321
        else if (role == Qt::FontRole) {
322
            App::DocumentObject* obj = v.getObject();
323
            App::DocumentObject* act = obj->getDocument()->getActiveObject();
324
            QFont font;
325
            font.setBold(obj==act);
326
            return static_cast<QVariant>(font);
327
        }
328

329
        return {};
330
    }
331

332
    // ------------------------------------------------------------------------
333

334
    TYPESYSTEM_SOURCE_ABSTRACT(Gui::DocumentModelIndex, Base::BaseClass)
335
    TYPESYSTEM_SOURCE_ABSTRACT(Gui::ApplicationIndex,Gui::DocumentModelIndex)
336
    TYPESYSTEM_SOURCE_ABSTRACT(Gui::DocumentIndex, Gui::DocumentModelIndex)
337
    TYPESYSTEM_SOURCE_ABSTRACT(Gui::ViewProviderIndex, Gui::DocumentModelIndex)
338

339
    struct DocumentModelP
340
    {
341
        DocumentModelP()
342
        { rootItem = new ApplicationIndex(); }
343
        ~DocumentModelP()
344
        { delete rootItem; }
345
        ApplicationIndex *rootItem;
346
    };
347
}
348

349
// -----------------------------------------------------------------
350

351
DocumentModel::DocumentModel(QObject* parent)
352
    : QAbstractItemModel(parent), d(new DocumentModelP)
353
{
354
    static bool inittype = false;
355
    if (!inittype) {
356
        inittype = true;
357
        DocumentModelIndex  ::init();
358
        ApplicationIndex    ::init();
359
        DocumentIndex       ::init();
360
        ViewProviderIndex   ::init();
361
    }
362

363
    //NOLINTBEGIN
364
    // Setup connections
365
    Application::Instance->signalNewDocument.connect(std::bind(&DocumentModel::slotNewDocument, this, sp::_1));
366
    Application::Instance->signalDeleteDocument.connect(std::bind(&DocumentModel::slotDeleteDocument, this, sp::_1));
367
    Application::Instance->signalRenameDocument.connect(std::bind(&DocumentModel::slotRenameDocument, this, sp::_1));
368
    Application::Instance->signalActiveDocument.connect(std::bind(&DocumentModel::slotActiveDocument, this, sp::_1));
369
    Application::Instance->signalRelabelDocument.connect(std::bind(&DocumentModel::slotRelabelDocument, this, sp::_1));
370
    //NOLINTEND
371
}
372

373
DocumentModel::~DocumentModel()
374
{
375
    delete d; d = nullptr;
376
}
377

378
void DocumentModel::slotNewDocument(const Gui::Document& Doc)
379
{
380
    //NOLINTBEGIN
381
    Doc.signalNewObject.connect(std::bind(&DocumentModel::slotNewObject, this, sp::_1));
382
    Doc.signalDeletedObject.connect(std::bind(&DocumentModel::slotDeleteObject, this, sp::_1));
383
    Doc.signalChangedObject.connect(std::bind(&DocumentModel::slotChangeObject, this, sp::_1, sp::_2));
384
    Doc.signalRelabelObject.connect(std::bind(&DocumentModel::slotRenameObject, this, sp::_1));
385
    Doc.signalActivatedObject.connect(std::bind(&DocumentModel::slotActiveObject, this, sp::_1));
386
    Doc.signalInEdit.connect(std::bind(&DocumentModel::slotInEdit, this, sp::_1));
387
    Doc.signalResetEdit.connect(std::bind(&DocumentModel::slotResetEdit, this, sp::_1));
388
    //NOLINTEND
389

390
    QModelIndex parent = createIndex(0,0,d->rootItem);
391
    int count_docs = d->rootItem->childCount();
392
    beginInsertRows(parent, count_docs, count_docs);
393
    d->rootItem->appendChild(new DocumentIndex(Doc));
394
    endInsertRows();
395
}
396

397
void DocumentModel::slotDeleteDocument(const Gui::Document& Doc)
398
{
399
    int row = d->rootItem->findChild(Doc);
400
    if (row > -1) {
401
        QModelIndex parent = createIndex(0,0,d->rootItem);
402
        beginRemoveRows(parent, row, row);
403
        DocumentModelIndex* item = d->rootItem->child(row);
404
        d->rootItem->removeChild(row);
405
        delete item;
406
        endRemoveRows();
407
    }
408
}
409

410
void DocumentModel::slotRenameDocument(const Gui::Document& Doc)
411
{
412
    Q_UNUSED(Doc);
413
    // do nothing here
414
}
415

416
void DocumentModel::slotRelabelDocument(const Gui::Document& Doc)
417
{
418
    int row = d->rootItem->findChild(Doc);
419
    if (row > -1) {
420
        QModelIndex parent = createIndex(0,0,d->rootItem);
421
        QModelIndex item = index (row, 0, parent);
422
        Q_EMIT dataChanged(item, item);
423
    }
424
}
425

426
void DocumentModel::slotActiveDocument(const Gui::Document& /*Doc*/)
427
{
428
    // don't know which was the previous active document, so check simply all
429
    QModelIndex parent = createIndex(0,0,d->rootItem);
430
    QModelIndex top = index (0, 0, parent);
431
    QModelIndex bottom = index (d->rootItem->childCount()-1, 0, parent);
432
    Q_EMIT dataChanged(top, bottom);
433
}
434

435
void DocumentModel::slotInEdit(const Gui::ViewProviderDocumentObject& v)
436
{
437
    Q_UNUSED(v);
438
}
439

440
void DocumentModel::slotResetEdit(const Gui::ViewProviderDocumentObject& v)
441
{
442
    Q_UNUSED(v);
443
}
444

445
void DocumentModel::slotNewObject(const Gui::ViewProviderDocumentObject& obj)
446
{
447
    App::Document* doc = obj.getObject()->getDocument();
448
    Gui::Document* gdc = Application::Instance->getDocument(doc);
449
    int row = d->rootItem->findChild(*gdc);
450
    if (row > -1) {
451
        auto index = static_cast<DocumentIndex*>(d->rootItem->child(row));
452
        QModelIndex parent = createIndex(index->row(),0,index);
453
        int count_obj = index->childCount();
454
        beginInsertRows(parent, count_obj, count_obj);
455
        index->appendChild(new ViewProviderIndex(obj, index));
456
        endInsertRows();
457
    }
458
}
459

460
void DocumentModel::slotDeleteObject(const Gui::ViewProviderDocumentObject& obj)
461
{
462
    App::Document* doc = obj.getObject()->getDocument();
463
    Gui::Document* gdc = Application::Instance->getDocument(doc);
464
    int row = d->rootItem->findChild(*gdc);
465
    if (row > -1) {
466
        auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row));
467
        QList<ViewProviderIndex*> views;
468
        doc_index->findViewProviders(obj, views);
469
        for (auto & view : views) {
470
            DocumentModelIndex* parentitem = view->parent();
471
            QModelIndex parent = createIndex(doc_index->row(), 0, parentitem);
472
            int row = view->row();
473
            beginRemoveRows(parent, row, row);
474
            parentitem->removeChild(row);
475
            delete view;
476
            endRemoveRows();
477
        }
478
    }
479
}
480

481
void DocumentModel::slotChangeObject(const Gui::ViewProviderDocumentObject& obj, const App::Property& Prop)
482
{
483
    App::DocumentObject* fea = obj.getObject();
484
    if (&fea->Label == &Prop) {
485
        App::Document* doc = fea->getDocument();
486
        Gui::Document* gdc = Application::Instance->getDocument(doc);
487
        int row = d->rootItem->findChild(*gdc);
488
        if (row > -1) {
489
            auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row));
490
            QList<ViewProviderIndex*> views;
491
            doc_index->findViewProviders(obj, views);
492
            for (const auto & view : std::as_const(views)) {
493
                DocumentModelIndex* parentitem = view->parent();
494
                QModelIndex parent = createIndex(0,0,parentitem);
495
                int row = view->row();
496
                QModelIndex item = index (row, 0, parent);
497
                Q_EMIT dataChanged(item, item);
498
            }
499
        }
500
    }
501
    else if (isPropertyLink(Prop)) {
502
        App::Document* doc = fea->getDocument();
503
        Gui::Document* gdc = Application::Instance->getDocument(doc);
504
        std::vector<ViewProviderDocumentObject*> views = claimChildren(*gdc, obj);
505

506
        int row = d->rootItem->findChild(*gdc);
507
        if (row > -1) {
508
            QList<DocumentModelIndex*> del_items;
509
            auto doc_index = static_cast<DocumentIndex*>(d->rootItem->child(row));
510
            for (const auto & view : views) {
511
                int row = doc_index->rowOfViewProvider(*view);
512
                // is it a top-level child in the document
513
                if (row >= 0) {
514
                    DocumentModelIndex* child = doc_index->child(row);
515
                    del_items.push_back(child);
516
                    QModelIndex parent = createIndex(doc_index->row(), 0, doc_index);
517
                    beginRemoveRows(parent, row, row);
518
                    doc_index->removeChild(row);
519
                    endRemoveRows();
520
                }
521
            }
522

523
            // get all occurrences of the view provider in the tree structure
524
            QList<ViewProviderIndex*> obj_index;
525
            doc_index->findViewProviders(obj, obj_index);
526
            for (const auto & it : std::as_const(obj_index)) {
527
                QModelIndex parent = createIndex(it->row(),0,it);
528
                int count_obj = it->childCount();
529
                beginRemoveRows(parent, 0, count_obj);
530
                // remove all children but do not yet delete them
531
                QList<DocumentModelIndex*> items = it->removeAll();
532
                endRemoveRows();
533

534
                beginInsertRows(parent, 0, (int)views.size());
535
                for (const auto & view : views) {
536
                    ViewProviderIndex* clone = doc_index->cloneViewProvider(*view);
537
                    it->appendChild(clone);
538
                }
539
                endInsertRows();
540

541
                del_items.append(items);
542
            }
543

544
            qDeleteAll(del_items);
545
        }
546
    }
547
}
548

549
void DocumentModel::slotRenameObject(const Gui::ViewProviderDocumentObject& obj)
550
{
551
    Q_UNUSED(obj);
552
    // renaming of objects not supported at the moment
553
}
554

555
void DocumentModel::slotActiveObject(const Gui::ViewProviderDocumentObject& obj)
556
{
557
    Q_UNUSED(obj);
558
    // do nothing here because this is automatically done by calling
559
    // ViewProviderIndex::data()
560
}
561

562
const Document* DocumentModel::getDocument(const QModelIndex& index) const
563
{
564
    if (!index.isValid())
565
        return nullptr;
566
    Base::BaseClass* item = nullptr;
567
    item = static_cast<Base::BaseClass*>(index.internalPointer());
568
    if (item->is<DocumentIndex>()) {
569
        const Gui::Document& d = static_cast<DocumentIndex*>(item)->d;
570
        return (&d);
571
    }
572

573
    return nullptr;
574
}
575

576
bool DocumentModel::isPropertyLink(const App::Property& prop) const
577
{
578
    if (prop.isDerivedFrom(App::PropertyLink::getClassTypeId()))
579
        return true;
580
    if (prop.isDerivedFrom(App::PropertyLinkSub::getClassTypeId()))
581
        return true;
582
    if (prop.isDerivedFrom(App::PropertyLinkList::getClassTypeId()))
583
        return true;
584
    if (prop.isDerivedFrom(App::PropertyLinkSubList::getClassTypeId()))
585
        return true;
586
    return false;
587
}
588

589
std::vector<ViewProviderDocumentObject*>
590
DocumentModel::claimChildren(const Gui::Document& doc, const ViewProviderDocumentObject& obj) const
591
{
592
    std::vector<ViewProviderDocumentObject*> views;
593
    std::vector<App::DocumentObject*> childs = obj.claimChildren();
594
    for (const auto & child : childs) {
595
        ViewProvider* view = doc.getViewProvider(child);
596
        if (view && view->isDerivedFrom<ViewProviderDocumentObject>())
597
            views.push_back(static_cast<ViewProviderDocumentObject*>(view));
598
    }
599

600
    return views;
601
}
602

603
int DocumentModel::columnCount (const QModelIndex & /*parent*/) const
604
{
605
    return 1;
606
}
607

608
QVariant DocumentModel::data (const QModelIndex & index, int role) const
609
{
610
    if (!index.isValid())
611
        return {};
612
    return static_cast<DocumentModelIndex*>(index.internalPointer())->data(role);
613
}
614

615
bool DocumentModel::setData(const QModelIndex& index, const QVariant & value, int role)
616
{
617
    if (!index.isValid())
618
        return false;
619
    return static_cast<DocumentModelIndex*>(index.internalPointer())->setData(value, role);
620
}
621

622
Qt::ItemFlags DocumentModel::flags(const QModelIndex &index) const
623
{
624
    //if (index.internalPointer() == d->rootItem)
625
    //    return Qt::ItemIsEnabled;
626
    //return QAbstractItemModel::flags(index);
627
    if (!index.isValid())
628
        return {};
629
    return static_cast<DocumentModelIndex*>(index.internalPointer())->flags();
630
}
631

632
QModelIndex DocumentModel::index (int row, int column, const QModelIndex & parent) const
633
{
634
    DocumentModelIndex* item = nullptr;
635
    if (!parent.isValid())
636
        item = d->rootItem;
637
    else
638
        item = static_cast<DocumentModelIndex*>(parent.internalPointer())->child(row);
639
    if (!item)
640
        return {};
641
    return createIndex(row, column, item);
642
}
643

644
QModelIndex DocumentModel::parent (const QModelIndex & index) const
645
{
646
    if (!index.isValid() || index.internalPointer() == d->rootItem)
647
        return {};
648
    DocumentModelIndex* item = nullptr;
649
    item = static_cast<DocumentModelIndex*>(index.internalPointer());
650
    DocumentModelIndex* parent = item->parent();
651
    return createIndex(parent->row(), 0, parent);
652
}
653

654
int DocumentModel::rowCount (const QModelIndex & parent) const
655
{
656
    if (!parent.isValid())
657
        return 1; // the root item
658
    DocumentModelIndex* item = nullptr;
659
    item = static_cast<DocumentModelIndex*>(parent.internalPointer());
660
    return item->childCount();
661
}
662

663
QVariant DocumentModel::headerData (int section, Qt::Orientation orientation, int role) const
664
{
665
    Q_UNUSED(section);
666
    if (orientation == Qt::Horizontal) {
667
        if (role != Qt::DisplayRole)
668
            return {};
669
        return tr("Labels & Attributes");
670
    }
671

672
    return {};
673
}
674

675
bool DocumentModel::setHeaderData (int, Qt::Orientation, const QVariant &, int)
676
{
677
    return false;
678
}
679

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.