File indexing completed on 2022-09-27 12:25:39

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool
0003     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "radselect.h"
0009 
0010 #include "radselectconfig.h"
0011 #include "radselectview.h"
0012 
0013 #include "ui_radselectprefdialog.h"
0014 
0015 #include <KActionCollection>
0016 #include <KConfig>
0017 #include <KConfigDialog>
0018 #include <KLocalizedString>
0019 #include <KStandardAction>
0020 #include <KStandardShortcut>
0021 #include <KXMLGUIFactory>
0022 
0023 #include <QDBusConnection>
0024 #include <QDBusInterface>
0025 #include <QDBusMessage>
0026 #include <QDragEnterEvent>
0027 #include <QDropEvent>
0028 #include <QMimeData>
0029 #include <QStatusBar>
0030 
0031 RadSelect::RadSelect()
0032 : KXmlGuiWindow()
0033 , m_view( new RadSelectView( this ) )
0034 {
0035   // accept dnd
0036   setAcceptDrops( true );
0037   setCentralWidget( m_view );  //This is the main widget
0038   setObjectName( QStringLiteral( "radselect" ) );
0039 
0040   KStandardAction::quit( this, SLOT(close()), actionCollection() );
0041   KStandardAction::preferences( this, SLOT(optionsPreferences()), actionCollection() );
0042 
0043   KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());
0044 
0045   statusBar()->show();
0046 
0047   // Apply the create the main window and ask the mainwindow to
0048   // automatically save settings if changed: window size, toolbar
0049   // position, icon size, etc.  Also to add actions for the statusbar
0050   // toolbar, and keybindings if necessary.
0051   setupGUI( Default, QStringLiteral("radselectui.rc") );
0052 
0053   // allow the view to change the statusbar
0054   connect( m_view, &RadSelectView::signalChangeStatusbar,
0055              this,   &RadSelect::changeStatusbar );
0056 
0057   if( ! QDBusConnection::sessionBus().isConnected() )
0058   {
0059     qDebug() << "Session Bus not found!!";
0060     m_dbusInterface = nullptr;
0061   }
0062   else
0063   {
0064     m_dbusInterface = new QDBusInterface(   QStringLiteral("org.kde.kiten"), QStringLiteral("/"), QLatin1String("")
0065                                           , QDBusConnection::sessionBus() );
0066   }
0067 
0068   // connect the search signal from the m_view with our dbus routines
0069   connect( m_view, &RadSelectView::kanjiSelected,
0070              this,   &RadSelect::sendSearch );
0071 }
0072 
0073 RadSelect::~RadSelect()
0074 {
0075   delete m_dbusInterface;
0076 }
0077 
0078 void RadSelect::changeStatusbar( const QString& text )
0079 {
0080   // display the text on the statusbar
0081   statusBar()->showMessage( text );
0082 }
0083 
0084 void RadSelect::dragEnterEvent( QDragEnterEvent *event )
0085 {
0086   if ( event->mimeData()->hasFormat( QStringLiteral("text/plain") ) )
0087   {
0088     event->acceptProposedAction();
0089   }
0090 }
0091 
0092 void RadSelect::dropEvent( QDropEvent *event )
0093 {
0094   QByteArray qba = event->mimeData()->data(QStringLiteral("text/plain"));
0095   if ( qba.size() > 0 )
0096   {
0097     loadSearchString( qba );
0098   }
0099 }
0100 
0101 void RadSelect::loadSearchString( const QString &searchString )
0102 {
0103   m_currentQuery = searchString;
0104   changeStatusbar( searchString );
0105   //TODO: Parse the strokes
0106   QString strokeStr = m_currentQuery.getProperty( QStringLiteral("S") );
0107   int min = 0;
0108   int max = 0;
0109   m_view->loadRadicals( m_currentQuery.getProperty( QStringLiteral("R") ), min, max );
0110 }
0111 
0112 void RadSelect::optionsPreferences()
0113 {
0114   if( KConfigDialog::showDialog( QStringLiteral("settings") ) )
0115   {
0116     return;
0117   }
0118 
0119   KConfigDialog *dialog = new KConfigDialog( this,QStringLiteral("settings"), RadSelectConfigSkeleton::self() );
0120   QWidget *preferences = new QWidget();
0121   Ui::radselectprefdialog layout;
0122   layout.setupUi( preferences );
0123   dialog->addPage( preferences, i18n( "Settings" ), QStringLiteral("help-contents") );
0124   connect( dialog, SIGNAL(settingsChanged(QString)),
0125            m_view,   SLOT(loadSettings()) );
0126   dialog->show();
0127 }
0128 
0129 void RadSelect::readProperties( const KConfigGroup &config )
0130 {
0131   //For resume
0132   loadSearchString( config.readPathEntry( "searchString", QString() ) );
0133 }
0134 
0135 void RadSelect::saveProperties( KConfigGroup &config )
0136 {
0137   //For suspend
0138   if ( ! m_currentQuery.isEmpty() )
0139   {
0140     config.writePathEntry( "searchString", m_currentQuery );
0141   }
0142 }
0143 
0144 //This one is triggered if the search button is used (or the widget interface
0145 //is in some other way given priority.
0146 void RadSelect::sendSearch( const QStringList& kanji )
0147 {
0148   if( kanji.size() == 0 )
0149   {
0150     return;
0151   }
0152 
0153   //This may need to be done differently for handling collisions
0154   m_currentQuery = kanji.at( 0 );
0155 
0156   changeStatusbar( m_currentQuery );
0157 
0158   if( m_dbusInterface && m_dbusInterface->isValid() )
0159   {
0160     QDBusMessage reply = m_dbusInterface->call(  QStringLiteral( "searchTextAndRaise" )
0161                                                , m_currentQuery.toString() );
0162     if( reply.type() == QDBusMessage::ErrorMessage )
0163     {
0164       qDebug() << "QDBus Error: " << reply.signature() << "<eoe>";
0165     }
0166   }
0167 }
0168 
0169