1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "consolewidget.h"
6#include <QDebug>
7#include <QMenu>
8static ConsoleWidget *ins{nullptr};
9
10class ConsoleWidgetPrivate
11{
12public:
13 QMenu *menu = nullptr;
14 QAction *consoleCopy = nullptr;
15 QAction *consolePaste = nullptr;
16};
17
18ConsoleWidget *ConsoleWidget::instance()
19{
20 if (!ins)
21 ins = new ConsoleWidget;
22 return ins;
23}
24
25ConsoleWidget::ConsoleWidget(QWidget *parent)
26 : QTermWidget(parent),
27 d(new ConsoleWidgetPrivate())
28{
29 setMargin(0);
30 setTerminalOpacity(0);
31 setForegroundRole(QPalette::ColorRole::Window);
32 setAutoFillBackground(true);
33 if (availableColorSchemes().contains("Linux"))
34 setColorScheme("Linux");
35 if (availableKeyBindings().contains("linux"))
36 setKeyBindings("linux");
37 setScrollBarPosition(QTermWidget::ScrollBarRight);
38 setTerminalSizeHint(false);
39 setAutoClose(false);
40 changeDir(QDir::homePath());
41 sendText("clear\n");
42
43 d->consoleCopy = new QAction(tr("copy"));
44 d->consolePaste = new QAction(tr("paste"));
45 QObject::connect(d->consoleCopy, &QAction::triggered, this, &QTermWidget::copyClipboard);
46 QObject::connect(d->consolePaste, &QAction::triggered, this, &QTermWidget::pasteClipboard);
47}
48
49ConsoleWidget::~ConsoleWidget()
50{
51 qInfo() << __FUNCTION__;
52}
53
54void ConsoleWidget::contextMenuEvent(QContextMenuEvent *event)
55{
56 if (nullptr == d->menu) {
57 d->menu = new QMenu(this);
58 d->menu->setParent(this);
59 d->menu->addAction(d->consoleCopy);
60 d->menu->addAction(d->consolePaste);
61 }
62 if (selectedText().isEmpty()) {
63 d->consoleCopy->setEnabled(false);
64 } else {
65 d->consoleCopy->setEnabled(true);
66 }
67 d->menu->exec(event->globalPos());
68}
69