File: textdocumentinspectorwidget.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (130 lines) | stat: -rw-r--r-- 5,360 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
  textdocumentinspectorwidget.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "textdocumentinspectorwidget.h"
#include "ui_textdocumentinspectorwidget.h"
#include "textdocumentmodel.h"

#include <common/objectmodel.h>
#include <common/objectbroker.h>
#include <common/objectid.h>
#include <common/endpoint.h>

#include <ui/contextmenuextension.h>

#include <QMenu>
#include <QTextDocument>

using namespace GammaRay;

TextDocumentInspectorWidget::TextDocumentInspectorWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TextDocumentInspectorWidget)
    , m_stateManager(this)
{
    ui->setupUi(this);

    ui->documentList->header()->setObjectName("documentListHeader");
    ui->documentList->setDeferredResizeMode(0, QHeaderView::Stretch);
    ui->documentList->setDeferredResizeMode(1, QHeaderView::Stretch);
    ui->documentList->setModel(ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.TextDocumentsModel")));
    ui->documentList->setSelectionModel(ObjectBroker::selectionModel(ui->documentList->model()));
    connect(ui->documentList->selectionModel(),
            &QItemSelectionModel::selectionChanged,
            this, &TextDocumentInspectorWidget::documentSelected);
    connect(ui->documentList, &QWidget::customContextMenuRequested, this, &TextDocumentInspectorWidget::documentContextMenu);

    ui->documentTree->header()->setObjectName("documentTreeHeader");
    ui->documentTree->setDeferredResizeMode(0, QHeaderView::Stretch);
    ui->documentTree->setDeferredResizeMode(1, QHeaderView::ResizeToContents);
    ui->documentTree->setModel(ObjectBroker::model(QStringLiteral(
        "com.kdab.GammaRay.TextDocumentModel")));
    ui->documentTree->setSelectionModel(ObjectBroker::selectionModel(ui->documentTree->model()));
    connect(ui->documentTree->selectionModel(),
            &QItemSelectionModel::selectionChanged,
            this, &TextDocumentInspectorWidget::documentElementSelected);

    ui->documentFormatView->header()->setObjectName("documentFormatViewHeader");
    ui->documentFormatView->setDeferredResizeMode(0, QHeaderView::ResizeToContents);
    ui->documentFormatView->setDeferredResizeMode(1, QHeaderView::Stretch);
    ui->documentFormatView->setDeferredResizeMode(2, QHeaderView::ResizeToContents);
    ui->documentFormatView->setModel(ObjectBroker::model(QStringLiteral(
        "com.kdab.GammaRay.TextDocumentFormatModel")));

    if (Endpoint::instance()->isRemoteClient()) // FIXME: content preview doesn't work remotely yet
        ui->tabWidget->hide();

    m_stateManager.setDefaultSizes(ui->mainSplitter, UISizeVector() << 280 << -1 << -1);
    m_stateManager.setDefaultSizes(ui->structureSplitter, UISizeVector() << "50%"
                                                                         << "50%");
}

TextDocumentInspectorWidget::~TextDocumentInspectorWidget() = default;

void TextDocumentInspectorWidget::documentSelected(const QItemSelection &selected,
                                                   const QItemSelection &deselected)
{
    Q_UNUSED(deselected);
    if (selected.isEmpty()) {
        return;
    }
    const QModelIndex selectedRow = selected.first().topLeft();
    QObject *selectedObj = selectedRow.data(ObjectModel::ObjectRole).value<QObject *>();
    QTextDocument *doc = qobject_cast<QTextDocument *>(selectedObj);

    if (m_currentDocument) {
        disconnect(m_currentDocument.data(), &QTextDocument::contentsChanged,
                   this, &TextDocumentInspectorWidget::documentContentChanged);
    }
    m_currentDocument = QPointer<QTextDocument>(doc);

    if (doc) {
        ui->documentView->setDocument(doc);
        connect(doc, &QTextDocument::contentsChanged, this, &TextDocumentInspectorWidget::documentContentChanged);
        documentContentChanged();
    }
}

void TextDocumentInspectorWidget::documentElementSelected(const QItemSelection &selected,
                                                          const QItemSelection &deselected)
{
    if (selected.isEmpty())
        return;

    Q_UNUSED(deselected);
    const QModelIndex selectedRow = selected.first().topLeft();

    const QRectF boundingBox = selectedRow.data(TextDocumentModel::BoundingBoxRole).toRectF();
    ui->documentView->setShowBoundingBox(boundingBox);
}

void TextDocumentInspectorWidget::documentContentChanged()
{
    ui->htmlView->setPlainText(m_currentDocument->toHtml());
}

void TextDocumentInspectorWidget::documentContextMenu(QPoint pos)
{
    const auto index = ui->documentList->indexAt(pos);
    if (!index.isValid())
        return;

    const auto objectId = index.data(ObjectModel::ObjectIdRole).value<ObjectId>();
    QMenu menu;
    ContextMenuExtension ext(objectId);
    ext.setLocation(ContextMenuExtension::Creation, index.data(ObjectModel::CreationLocationRole).value<SourceLocation>());
    ext.setLocation(ContextMenuExtension::Declaration, index.data(ObjectModel::DeclarationLocationRole).value<SourceLocation>());
    ext.populateMenu(&menu);

    menu.exec(ui->documentList->viewport()->mapToGlobal(pos));
}