mirror of https://gitee.com/cxasm/notepad--.git
plugin/opencc-demo: 提升简繁转换示例插件的可用性
This commit is contained in:
parent
a5fab253e3
commit
824d367191
|
@ -1,14 +1,19 @@
|
|||
#include "ndd_plugin_implement.h"
|
||||
#include "ndd_plugin_implement.h"
|
||||
#include "ui_ndd_plugin_implement.h"
|
||||
|
||||
#include <qsciscintilla.h>
|
||||
#include <QDebug>
|
||||
#include <QDockWidget>
|
||||
#include <QSplitter>
|
||||
#include <QThreadPool>
|
||||
#include <qpushbutton.h>
|
||||
#include <qsciscintilla.h>
|
||||
|
||||
NddPluginImplement::NddPluginImplement(QWidget *parent, QsciScintilla *pEdit) : QMainWindow (parent)
|
||||
, ui(new Ui::NddPluginImplement)
|
||||
, currentEdit(pEdit)
|
||||
NddPluginImplement::NddPluginImplement(QWidget *parent, QsciScintilla *pEdit) : QMainWindow(parent)
|
||||
, currentWidget(parent)
|
||||
, currentEdit(pEdit)
|
||||
, ui(new Ui::NddPluginImplement)
|
||||
, m_dockWidget(new QDockWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
@ -23,6 +28,7 @@ NddPluginImplement::NddPluginImplement(QWidget *parent, QsciScintilla *pEdit) :
|
|||
#if WIN32
|
||||
connect(ui->pushButton, &QPushButton::clicked, this, [&](){
|
||||
task = new OpenCCTask(this, ui->textEdit->toPlainText());
|
||||
task->setCn2Tn(ui->radioCnTn->isChecked());
|
||||
connect(task, &OpenCCTask::complete, this, [=](const QString text) {
|
||||
this->ui->textBrowser->setText(text);
|
||||
});
|
||||
|
@ -39,14 +45,80 @@ NddPluginImplement::~NddPluginImplement()
|
|||
|
||||
}
|
||||
|
||||
void NddPluginImplement::on_textEdit_textChanged()
|
||||
void NddPluginImplement::setMenuActions(QMenu *menu)
|
||||
{
|
||||
connect(menu->addAction("Show Ui"), &QAction::triggered, this, [=](){
|
||||
// on_pushButton_clicked();
|
||||
this->setParent(nullptr);
|
||||
this->show();
|
||||
this->resize(QSize(570,400));
|
||||
this->ui->tabWidget->setCurrentWidget(ui->tab);
|
||||
});
|
||||
|
||||
connect(menu->addAction("Show Sider"), &QAction::triggered, this, [=](){
|
||||
auto mainWindow = dynamic_cast<QMainWindow*>(currentWidget);
|
||||
if(!mainWindow) {
|
||||
qDebug() << "None";
|
||||
return;
|
||||
}
|
||||
|
||||
m_dockWidget->setMinimumSize(100, 200);
|
||||
mainWindow->addDockWidget(Qt::RightDockWidgetArea, m_dockWidget);
|
||||
|
||||
if (!m_dockWidget->isVisible())
|
||||
m_dockWidget->show();
|
||||
|
||||
m_dockWidget->setWidget(this);
|
||||
|
||||
ui->tabWidget->setCurrentWidget(ui->tab_2);
|
||||
});
|
||||
}
|
||||
|
||||
void NddPluginImplement::convertCn2TnInEditBrowser(bool cn2tn)
|
||||
{
|
||||
#if WIN32
|
||||
#else
|
||||
task = new OpenCCTask(this, ui->textEdit->toPlainText());
|
||||
task->setCn2Tn(cn2tn);
|
||||
connect(task, &OpenCCTask::complete, this, [=](const QString text) {
|
||||
this->ui->textBrowser->setText(text);
|
||||
});
|
||||
#if WIN32
|
||||
task->run();
|
||||
task->deleteLater();
|
||||
#else
|
||||
QThreadPool::globalInstance()->start(task);
|
||||
#endif
|
||||
}
|
||||
|
||||
void NddPluginImplement::convertCn2TnInCurrentEdit(bool cn2tn)
|
||||
{
|
||||
task = new OpenCCTask(this, getCurrentEditFunc()->text());
|
||||
task->setCn2Tn(cn2tn);
|
||||
connect(task, &OpenCCTask::complete, this, [=](const QString text) {
|
||||
getCurrentEditFunc()->setText(text);
|
||||
});
|
||||
#if WIN32
|
||||
task->run();
|
||||
task->deleteLater();
|
||||
#else
|
||||
QThreadPool::globalInstance()->start(task);
|
||||
#endif
|
||||
}
|
||||
|
||||
void NddPluginImplement::on_textEdit_textChanged()
|
||||
{
|
||||
#if WIN32
|
||||
// 不采用实时任务
|
||||
#else
|
||||
convertCn2TnInEditBrowser(this->ui->radioCnTn->isChecked());
|
||||
#endif
|
||||
}
|
||||
|
||||
void NddPluginImplement::on_pushButtonCn2Tn_clicked(bool checked)
|
||||
{
|
||||
convertCn2TnInCurrentEdit(true);
|
||||
}
|
||||
|
||||
void NddPluginImplement::on_pushButtonTn2Cn_clicked(bool checked)
|
||||
{
|
||||
convertCn2TnInCurrentEdit(false);
|
||||
}
|
||||
|
|
|
@ -2,9 +2,7 @@
|
|||
#define NDD_PLUGIN_IMPLEMENT_H
|
||||
|
||||
#include "opencctask.h"
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QWidget>
|
||||
|
||||
class QsciScintilla;
|
||||
namespace Ui {
|
||||
|
@ -18,12 +16,30 @@ public:
|
|||
explicit NddPluginImplement(QWidget *parent = nullptr, QsciScintilla *pEdit = nullptr);
|
||||
~NddPluginImplement();
|
||||
|
||||
void setMenuActions(QMenu *menu);
|
||||
void setCurrentEditFunc(std::function<QsciScintilla* ()> func) {
|
||||
getCurrentEditFunc = func;
|
||||
}
|
||||
|
||||
void convertCn2TnInEditBrowser(bool cn2tn);
|
||||
void convertCn2TnInCurrentEdit(bool cn2tn);
|
||||
|
||||
private slots:
|
||||
void on_textEdit_textChanged();
|
||||
|
||||
void on_pushButtonCn2Tn_clicked(bool checked);
|
||||
|
||||
void on_pushButtonTn2Cn_clicked(bool checked);
|
||||
|
||||
private:
|
||||
// 目前看来需要准备一个完整内部状态
|
||||
QWidget *currentWidget;
|
||||
QsciScintilla *currentEdit;
|
||||
std::function<QsciScintilla* ()> getCurrentEditFunc;
|
||||
|
||||
private:
|
||||
Ui::NddPluginImplement *ui;
|
||||
QsciScintilla *currentEdit;
|
||||
QDockWidget *m_dockWidget;
|
||||
|
||||
OpenCCTask *task;
|
||||
};
|
||||
|
|
|
@ -6,30 +6,171 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>554</width>
|
||||
<height>379</height>
|
||||
<width>570</width>
|
||||
<height>400</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>简繁转换面板 - Demo</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QTextBrowser" name="textBrowser"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTextEdit" name="textEdit"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="text">
|
||||
<string>Convert</string>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab">
|
||||
<attribute name="title">
|
||||
<string>Tab 1</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QTextEdit" name="textEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTextBrowser" name="textBrowser"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QRadioButton" name="radioCnTn">
|
||||
<property name="text">
|
||||
<string>简>繁</string>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QRadioButton" name="radioTnCn">
|
||||
<property name="text">
|
||||
<string>繁>简</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="text">
|
||||
<string>Convert</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
<attribute name="title">
|
||||
<string>Tab 2</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="1" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>繁 > 简:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButtonTn2Cn">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>繁简转换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>简 > 繁:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButtonCn2Tn">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>简繁转换</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
|
@ -39,8 +180,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>554</width>
|
||||
<height>23</height>
|
||||
<width>570</width>
|
||||
<height>27</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
|
|
|
@ -8,21 +8,36 @@ OpenCCTask::OpenCCTask(QObject *parent, QString text) : QObject(parent)
|
|||
|
||||
}
|
||||
|
||||
void OpenCCTask::run()
|
||||
{
|
||||
opencc::SimpleConverter converter(cn2Tn()?"s2t.json":"t2s.json");
|
||||
std::string s = converter.Convert(m_text.toStdString().data());
|
||||
QString result = QString::fromStdString(s);
|
||||
emit complete(result);
|
||||
}
|
||||
|
||||
QString OpenCCTask::text() const
|
||||
{
|
||||
return m_text;
|
||||
}
|
||||
|
||||
void OpenCCTask::setText(const QString &text)
|
||||
void OpenCCTask::setText(const QString &newText)
|
||||
{
|
||||
m_text = text;
|
||||
if (m_text == newText)
|
||||
return;
|
||||
m_text = newText;
|
||||
emit textChanged();
|
||||
}
|
||||
|
||||
void OpenCCTask::run()
|
||||
bool OpenCCTask::cn2Tn() const
|
||||
{
|
||||
opencc::SimpleConverter converter("s2t.json");
|
||||
|
||||
std::string s = converter.Convert(m_text.toStdString().data());
|
||||
QString result = QString::fromStdString(s);
|
||||
emit complete(result);
|
||||
return m_cn2Tn;
|
||||
}
|
||||
|
||||
void OpenCCTask::setCn2Tn(bool newCn2Tn)
|
||||
{
|
||||
if (m_cn2Tn == newCn2Tn)
|
||||
return;
|
||||
m_cn2Tn = newCn2Tn;
|
||||
emit cn2TnChanged();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,9 @@
|
|||
class OpenCCTask : public QObject , public QRunnable
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
|
||||
Q_PROPERTY(bool cn2Tn READ cn2Tn WRITE setCn2Tn NOTIFY cn2TnChanged)
|
||||
|
||||
public:
|
||||
explicit OpenCCTask(QObject *parent = nullptr, QString text = "");
|
||||
|
||||
|
@ -18,18 +21,26 @@ public:
|
|||
|
||||
// void setTaskType(TaskType type = NormalType);
|
||||
|
||||
QString text() const;
|
||||
void setText(const QString &text);
|
||||
|
||||
public:
|
||||
// QRunnable interface
|
||||
void run() override;
|
||||
|
||||
|
||||
signals:
|
||||
void complete(const QString s);
|
||||
void textChanged();
|
||||
void cn2TnChanged();
|
||||
|
||||
// QRunnable interface
|
||||
public:
|
||||
void run() override;
|
||||
QString text() const;
|
||||
void setText(const QString &newText);
|
||||
bool cn2Tn() const;
|
||||
void setCn2Tn(bool newCn2Tn);
|
||||
|
||||
private:
|
||||
QString m_text;
|
||||
bool m_cn2Tn;
|
||||
};
|
||||
|
||||
#endif // OPENCCTASK_H
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
#include "ndd_plugin_implement.h"
|
||||
|
||||
|
||||
NOTEPAD_PLUGIN_METADATA_IDENTIFY(u8"简繁转换面板", "0.1", "zinface", u8"一个简单的简繁转换面板", "");
|
||||
NOTEPAD_PLUGIN_METADATA_IDENTIFY_V1(u8"简繁转换面板", "0.1", "zinface", u8"一个简单的简繁转换面板", "");
|
||||
|
||||
NOTEPAD_PLUGIN_METADATA_IMPLEMENT(NddPluginImplement, true);
|
||||
NOTEPAD_PLUGIN_METADATA_IMPLEMENT_V1(NddPluginImplement, false);
|
||||
|
|
Loading…
Reference in New Issue