1 /*
2   networkreplywidget.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2019-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include "networkreplywidget.h"
30 #include "clientnetworkreplymodel.h"
31 #include "networkreplymodeldefs.h"
32 #include "ui_networkreplywidget.h"
33 
34 #include <ui/contextmenuextension.h>
35 
36 #include <common/objectbroker.h>
37 
38 #include <QClipboard>
39 #include <QGuiApplication>
40 #include <QMenu>
41 
42 using namespace GammaRay;
43 
NetworkReplyWidget(QWidget * parent)44 NetworkReplyWidget::NetworkReplyWidget(QWidget* parent)
45     : QWidget(parent)
46     , ui(new Ui::NetworkReplyWidget)
47 {
48     ui->setupUi(this);
49 
50     auto srcModel = ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.NetworkReplyModel"));
51     auto proxy = new ClientNetworkReplyModel(this);
52     proxy->setSourceModel(srcModel);
53     ui->replyView->setModel(proxy);
54     ui->replyView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
55     ui->replyView->expandAll();
56 
57     // auto-expand parents of new replies
58     connect(proxy, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int, int) {
59         if (!parent.isValid()) {
60             return;
61         }
62         ui->replyView->expand(parent);
63     });
64 
65     connect(ui->replyView, &QWidget::customContextMenuRequested, this, &NetworkReplyWidget::contextMenu);
66 
67 }
68 
69 NetworkReplyWidget::~NetworkReplyWidget() = default;
70 
contextMenu(QPoint pos)71 void NetworkReplyWidget::contextMenu(QPoint pos)
72 {
73     const auto index = ui->replyView->indexAt(pos);
74     if (!index.isValid())
75         return;
76 
77     const auto objectId = index.sibling(index.row(), NetworkReplyModelColumn::ObjectColumn).data(NetworkReplyModelRole::ObjectIdRole).value<ObjectId>();
78     const auto url = index.sibling(index.row(), NetworkReplyModelColumn::UrlColumn).data(Qt::DisplayRole).toString();
79 
80     QMenu menu;
81     if (!url.isEmpty()) {
82         auto action = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), tr("Copy URL"));
83         connect(action, &QAction::triggered, this, [url]() {
84             QGuiApplication::clipboard()->setText(url);
85         });
86         menu.addSeparator();
87     }
88 
89     ContextMenuExtension ext(objectId);
90     ext.populateMenu(&menu);
91     menu.exec(ui->replyView->viewport()->mapToGlobal(pos));
92 }
93