File: radselect.cpp

package info (click to toggle)
kiten 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,156 kB
  • sloc: cpp: 6,079; xml: 239; makefile: 8; sh: 2
file content (152 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "radselect.h"

#include "radselectconfig.h"
#include "radselectview.h"

#include "ui_radselectprefdialog.h"

#include <KActionCollection>
#include <KConfigDialog>
#include <KStandardAction>
#include <KStandardShortcut>
#include <KXMLGUIFactory>

#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDropEvent>
#include <QMimeData>
#include <QStatusBar>

RadSelect::RadSelect()
    : KXmlGuiWindow()
    , m_view(new RadSelectView(this))
{
    // accept dnd
    setAcceptDrops(true);
    setCentralWidget(m_view); // This is the main widget
    setObjectName(QStringLiteral("radselect"));

    KStandardAction::quit(this, SLOT(close()), actionCollection());
    KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection());

    KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());

    statusBar()->show();

    // Apply the create the main window and ask the mainwindow to
    // automatically save settings if changed: window size, toolbar
    // position, icon size, etc.  Also to add actions for the statusbar
    // toolbar, and keybindings if necessary.
    setupGUI(Keys | StatusBar | Save | Create, QStringLiteral("radselectui.rc"));

    // allow the view to change the statusbar
    connect(m_view, &RadSelectView::signalChangeStatusbar, this, &RadSelect::changeStatusbar);

    if (!QDBusConnection::sessionBus().isConnected()) {
        qDebug() << "Session Bus not found!!";
        m_dbusInterface = nullptr;
    } else {
        m_dbusInterface = new QDBusInterface(QStringLiteral("org.kde.kiten"), QStringLiteral("/"), QLatin1String(""), QDBusConnection::sessionBus());
    }

    // connect the search signal from the m_view with our dbus routines
    connect(m_view, &RadSelectView::kanjiSelected, this, &RadSelect::sendSearch);
}

RadSelect::~RadSelect()
{
    delete m_dbusInterface;
}

void RadSelect::changeStatusbar(const QString &text)
{
    // display the text on the statusbar
    statusBar()->showMessage(text);
}

void RadSelect::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasFormat(QStringLiteral("text/plain"))) {
        event->acceptProposedAction();
    }
}

void RadSelect::dropEvent(QDropEvent *event)
{
    QByteArray qba = event->mimeData()->data(QStringLiteral("text/plain"));
    if (qba.size() > 0) {
        loadSearchString(QString::fromUtf8(qba));
    }
}

void RadSelect::loadSearchString(const QString &searchString)
{
    m_currentQuery = searchString;
    changeStatusbar(searchString);
    // TODO: Parse the strokes
    QString strokeStr = m_currentQuery.getProperty(QStringLiteral("S"));
    int min = 0;
    int max = 0;
    m_view->loadRadicals(m_currentQuery.getProperty(QStringLiteral("R")), min, max);
}

void RadSelect::optionsPreferences()
{
    if (KConfigDialog::showDialog(QStringLiteral("settings"))) {
        return;
    }

    KConfigDialog *dialog = new KConfigDialog(this, QStringLiteral("settings"), RadSelectConfigSkeleton::self());
    dialog->setFaceType(KPageDialog::FaceType::Plain);
    auto preferences = new QWidget();
    Ui::radselectprefdialog layout;
    layout.setupUi(preferences);
    dialog->addPage(preferences, QString(), QStringLiteral("help-contents"));
    connect(dialog, SIGNAL(settingsChanged(QString)), m_view, SLOT(loadSettings()));
    dialog->show();
}

void RadSelect::readProperties(const KConfigGroup &config)
{
    // For resume
    loadSearchString(config.readPathEntry("searchString", QString()));
}

void RadSelect::saveProperties(KConfigGroup &config)
{
    // For suspend
    if (!m_currentQuery.isEmpty()) {
        config.writePathEntry("searchString", m_currentQuery);
    }
}

// This one is triggered if the search button is used (or the widget interface
// is in some other way given priority.
void RadSelect::sendSearch(const QStringList &kanji)
{
    if (kanji.empty()) {
        return;
    }

    // This may need to be done differently for handling collisions
    m_currentQuery = kanji.at(0);

    changeStatusbar(m_currentQuery);

    if (m_dbusInterface && m_dbusInterface->isValid()) {
        QDBusMessage reply = m_dbusInterface->call(QStringLiteral("searchTextAndRaise"), m_currentQuery.toString());
        if (reply.type() == QDBusMessage::ErrorMessage) {
            qDebug() << "QDBus Error: " << reply.signature() << "<eoe>";
        }
    }
}

#include "moc_radselect.cpp"