mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-05-30 02:49:12 -04:00
116 lines
3.1 KiB
C++
116 lines
3.1 KiB
C++
// Copyright (C) 2024-2026 Petr Mironychev
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "ChatView.hpp"
|
|
|
|
#include <QQmlComponent>
|
|
#include <QQmlContext>
|
|
#include <QQmlEngine>
|
|
#include <QQuickItem>
|
|
#include <QSettings>
|
|
#include <QVariantMap>
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
#include <coreplugin/actionmanager/command.h>
|
|
#include <logger/Logger.hpp>
|
|
|
|
#include "QodeAssistConstants.hpp"
|
|
|
|
namespace {
|
|
constexpr Qt::WindowFlags baseFlags = Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
|
|
| Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint
|
|
| Qt::WindowCloseButtonHint;
|
|
}
|
|
|
|
namespace QodeAssist::Chat {
|
|
|
|
ChatView::ChatView(QQmlEngine* engine)
|
|
: QQuickView{engine, nullptr}
|
|
, m_isPin(false)
|
|
{
|
|
setTitle("QodeAssist Chat");
|
|
/// @note setup quick view content
|
|
{
|
|
auto context = new QQmlContext{engine, this};
|
|
context->setContextProperty("_chatview", this);
|
|
|
|
auto component = new QQmlComponent{engine, QUrl{"qrc:/qt/qml/ChatView/qml/RootItem.qml"}, this};
|
|
auto rootItem = component->create(context);
|
|
|
|
setContent(component->url(), component, rootItem);
|
|
}
|
|
setResizeMode(QQuickView::SizeRootObjectToView);
|
|
setMinimumSize({400, 300});
|
|
setFlags(baseFlags);
|
|
|
|
bindCommandShortcut("QodeAssist.CloseChatView", [this] { close(); });
|
|
bindCommandShortcut(Constants::QODE_ASSIST_CHAT_SEND_MESSAGE, [this] {
|
|
QMetaObject::invokeMethod(rootObject(), "sendChatMessage");
|
|
});
|
|
bindCommandShortcut(Constants::QODE_ASSIST_CHAT_CLEAR_SESSION, [this] {
|
|
QMetaObject::invokeMethod(rootObject(), "clearChat");
|
|
});
|
|
|
|
restoreSettings();
|
|
}
|
|
|
|
void ChatView::bindCommandShortcut(Utils::Id commandId,
|
|
const std::function<void()> &onActivated)
|
|
{
|
|
auto command = Core::ActionManager::command(commandId);
|
|
if (!command)
|
|
return;
|
|
|
|
auto shortcut = new QShortcut(command->keySequence(), this);
|
|
connect(shortcut, &QShortcut::activated, this, onActivated);
|
|
connect(command, &Core::Command::keySequenceChanged, shortcut, [command, shortcut]() {
|
|
shortcut->setKey(command->keySequence());
|
|
});
|
|
}
|
|
|
|
void ChatView::closeEvent(QCloseEvent *event)
|
|
{
|
|
saveSettings();
|
|
event->accept();
|
|
}
|
|
|
|
void ChatView::saveSettings()
|
|
{
|
|
QSettings settings;
|
|
settings.setValue("QodeAssist/ChatView/geometry", geometry());
|
|
settings.setValue("QodeAssist/ChatView/pinned", m_isPin);
|
|
}
|
|
|
|
void ChatView::restoreSettings()
|
|
{
|
|
QSettings settings;
|
|
const QRect savedGeometry
|
|
= settings.value("QodeAssist/ChatView/geometry", QRect(100, 100, 800, 600)).toRect();
|
|
setGeometry(savedGeometry);
|
|
|
|
const bool pinned = settings.value("QodeAssist/ChatView/pinned", false).toBool();
|
|
setIsPin(pinned);
|
|
}
|
|
|
|
bool ChatView::isPin() const
|
|
{
|
|
return m_isPin;
|
|
}
|
|
|
|
void ChatView::setIsPin(bool newIsPin)
|
|
{
|
|
if (m_isPin == newIsPin)
|
|
return;
|
|
m_isPin = newIsPin;
|
|
|
|
if (m_isPin) {
|
|
setFlags(baseFlags | Qt::WindowStaysOnTopHint);
|
|
} else {
|
|
setFlags(baseFlags);
|
|
}
|
|
|
|
emit isPinChanged();
|
|
}
|
|
|
|
} // namespace QodeAssist::Chat
|