Add qt support for widgets addons and guiwrapper

This commit is contained in:
Weng Xuetian 2023-11-17 11:52:40 -08:00
parent d7787f570b
commit 10a67ed28d
No known key found for this signature in database
GPG Key ID: 8E8B898CBF2412F9
25 changed files with 1304 additions and 4 deletions

View File

@ -6,8 +6,8 @@
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef FCITXQT5_GUIWRAPPER_MAINWINDOW_H
#define FCITXQT5_GUIWRAPPER_MAINWINDOW_H
#ifndef FCITX5QT_GUIWRAPPER_MAINWINDOW_H
#define FCITX5QT_GUIWRAPPER_MAINWINDOW_H
#include <QDialog>

View File

@ -22,7 +22,8 @@ WrapperApp::WrapperApp(int &argc, char **argv)
mainWindow_(0) {
FcitxQtConfigUIWidget *widget = 0;
setApplicationName(QLatin1String("fcitx5-qt5-gui-wrapper"));
setApplicationName(QLatin1String(
"fcitx5-qt" QT_STRINGIFY(QT_VERSION_MAJOR) "-gui-wrapper"));
setApplicationVersion(QLatin1String(FCITX5_QT_VERSION));
setOrganizationDomain("fcitx.org");

View File

@ -166,7 +166,7 @@ FcitxQtKeySequenceWidget::FcitxQtKeySequenceWidget(QWidget *parent)
void FcitxQtKeySequenceWidgetPrivate::init() {
layout_ = new QHBoxLayout(q);
layout_->setMargin(0);
layout_->setContentsMargins(0, 0, 0, 0);
keyButton_ = new FcitxQtKeySequenceButton(this, q);
keyButton_->setFocusPolicy(Qt::StrongFocus);

View File

@ -10,5 +10,7 @@ add_subdirectory(dbusaddons)
add_subdirectory(platforminputcontext)
if(NOT BUILD_ONLY_PLUGIN)
add_subdirectory(guiwrapper)
add_subdirectory(widgetsaddons)
add_subdirectory(immodule-probing)
endif()

View File

@ -0,0 +1,25 @@
set(QT_WRAPPER_SRCS
main.cpp
wrapperapp.cpp
mainwindow.cpp
)
add_executable(fcitx5-qt6-gui-wrapper ${QT_WRAPPER_SRCS})
set_target_properties(fcitx5-qt6-gui-wrapper
PROPERTIES AUTOMOC TRUE AUTOUIC TRUE AUTOUIC_OPTIONS "-tr=fcitx::tr2fcitx;--include=fcitxqti18nhelper.h")
target_link_libraries(fcitx5-qt6-gui-wrapper
Fcitx5::Utils
Qt6::Core
Qt6::Gui
Qt6::Widgets
Fcitx5Qt6::DBusAddons
Fcitx5Qt6::WidgetsAddons
)
install(TARGETS fcitx5-qt6-gui-wrapper DESTINATION "${CMAKE_INSTALL_LIBEXECDIR}")
configure_file(org.fcitx.fcitx5-qt6-gui-wrapper.desktop.in.in ${CMAKE_CURRENT_BINARY_DIR}/org.fcitx.fcitx5-qt6-gui-wrapper.desktop.in @ONLY)
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/org.fcitx.fcitx5-qt6-gui-wrapper.desktop.in
org.fcitx.fcitx5-qt6-gui-wrapper.desktop)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.fcitx.fcitx5-qt6-gui-wrapper.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)

1
qt6/guiwrapper/main.cpp Symbolic link
View File

@ -0,0 +1 @@
../../qt5/guiwrapper/main.cpp

View File

@ -0,0 +1 @@
../../qt5/guiwrapper/mainwindow.cpp

1
qt6/guiwrapper/mainwindow.h Symbolic link
View File

@ -0,0 +1 @@
../../qt5/guiwrapper/mainwindow.h

View File

@ -0,0 +1 @@
../../qt5/guiwrapper/mainwindow.ui

View File

@ -0,0 +1,9 @@
[Desktop Entry]
Name=Fcitx 5 Qt6 Gui Wrapper
GenericName=Input Method Configuration helper
Comment=Load configuration plugin for Fcitx Addon
Icon=fcitx
Exec=@CMAKE_INSTALL_FULL_LIBEXECDIR@/fcitx5-qt6-gui-wrapper
Type=Application
Categories=Settings;
NoDisplay=true

View File

@ -0,0 +1 @@
../../qt5/guiwrapper/wrapperapp.cpp

1
qt6/guiwrapper/wrapperapp.h Symbolic link
View File

@ -0,0 +1 @@
../../qt5/guiwrapper/wrapperapp.h

View File

@ -0,0 +1,70 @@
ecm_setup_version(PROJECT VARIABLE_PREFIX FCITX5QT6WIDGETSADDONS
VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/fcitx5qt6widgetsaddons_version.h"
PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/Fcitx5Qt6WidgetsAddonsConfigVersion.cmake"
SOVERSION 2)
# create a Config.cmake and a ConfigVersion.cmake file and install them
set(CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/Fcitx5Qt6WidgetsAddons")
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/Fcitx5Qt6WidgetsAddonsConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/Fcitx5Qt6WidgetsAddonsConfig.cmake"
INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Fcitx5Qt6WidgetsAddonsConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/Fcitx5Qt6WidgetsAddonsConfigVersion.cmake"
DESTINATION "${CMAKECONFIG_INSTALL_DIR}"
COMPONENT Devel )
install(EXPORT Fcitx5Qt6WidgetsAddonsTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE Fcitx5Qt6WidgetsAddonsTargets.cmake NAMESPACE Fcitx5Qt6:: )
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/fcitx5qt6widgetsaddons_version.h
DESTINATION ${Fcitx5Qt6_INCLUDE_INSTALL_DIR} COMPONENT Devel )
set(widgetsaddons_SOURCES
fcitxqtconfiguifactory.cpp
fcitxqtconfiguiplugin.cpp
fcitxqtconfiguiwidget.cpp
fcitxqtkeysequencewidget.cpp
)
set(widgetsaddons_HEADERS
fcitxqtconfiguifactory.h
fcitxqtconfiguiplugin.h
fcitxqtconfiguiwidget.h
fcitxqtkeysequencewidget.h
fcitxqti18nhelper.h
)
set(fcitxqtwidgetsaddons_INCLUDE_DIRS
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${FCITX54_FCITX5_UTILS_INCLUDE_DIRS}
)
add_library(Fcitx5Qt6WidgetsAddons SHARED ${widgetsaddons_SOURCES})
generate_export_header(Fcitx5Qt6WidgetsAddons BASE_NAME Fcitx5Qt6WidgetsAddons)
add_library(Fcitx5Qt6::WidgetsAddons ALIAS Fcitx5Qt6WidgetsAddons)
target_include_directories(Fcitx5Qt6WidgetsAddons PUBLIC "$<BUILD_INTERFACE:${fcitxqtwidgetsaddons_INCLUDE_DIRS}>")
target_include_directories(Fcitx5Qt6WidgetsAddons INTERFACE "$<INSTALL_INTERFACE:${Fcitx5Qt6_INCLUDE_INSTALL_DIR}/Fcitx5QtWidgetsAddons>")
set_target_properties(Fcitx5Qt6WidgetsAddons
PROPERTIES VERSION ${FCITX5QT6WIDGETSADDONS_VERSION}
AUTOMOC TRUE
SOVERSION ${FCITX5QT6WIDGETSADDONS_SOVERSION}
EXPORT_NAME WidgetsAddons
)
target_link_libraries(Fcitx5Qt6WidgetsAddons
PUBLIC
Fcitx5::Utils
Qt6::Core
Qt6::Widgets
)
install(TARGETS Fcitx5Qt6WidgetsAddons EXPORT Fcitx5Qt6WidgetsAddonsTargets LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(FILES ${widgetsaddons_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/fcitx5qt6widgetsaddons_export.h
DESTINATION "${Fcitx5Qt6_INCLUDE_INSTALL_DIR}/Fcitx5QtWidgetsAddons")

View File

@ -0,0 +1,16 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(Fcitx5Utils)
find_dependency(Qt6Core @REQUIRED_QT6_VERSION@)
find_dependency(Qt6Gui @REQUIRED_QT6_VERSION@)
find_dependency(Qt6Widgets @REQUIRED_QT6_VERSION@)
include("${CMAKE_CURRENT_LIST_DIR}/Fcitx5Qt6WidgetsAddonsTargets.cmake")
if(NOT TARGET Fcitx5Qt6::gui-wrapper)
add_executable(Fcitx5Qt6::gui-wrapper IMPORTED)
set_target_properties(Fcitx5Qt6::gui-wrapper PROPERTIES
IMPORTED_LOCATION "@CMAKE_INSTALL_FULL_LIBEXECDIR@/fcitx5-qt6-gui-wrapper")
endif()

View File

@ -0,0 +1,109 @@
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "fcitxqtconfiguifactory.h"
#include "fcitxqtconfiguifactory_p.h"
#include "fcitxqtconfiguiplugin.h"
#include <QDebug>
#include <QDir>
#include <QLibrary>
#include <QPluginLoader>
#include <QStandardPaths>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/standardpath.h>
namespace fcitx {
namespace {
constexpr char addonConfigPrefix[] = "fcitx://config/addon/";
QString normalizePath(const QString &file) {
auto path = file;
if (path.startsWith(addonConfigPrefix)) {
path.remove(0, sizeof(addonConfigPrefix) - 1);
}
return path;
}
} // namespace
FcitxQtConfigUIFactoryPrivate::FcitxQtConfigUIFactoryPrivate(
FcitxQtConfigUIFactory *factory)
: QObject(factory), q_ptr(factory) {}
FcitxQtConfigUIFactoryPrivate::~FcitxQtConfigUIFactoryPrivate() {}
FcitxQtConfigUIFactory::FcitxQtConfigUIFactory(QObject *parent)
: QObject(parent), d_ptr(new FcitxQtConfigUIFactoryPrivate(this)) {
Q_D(FcitxQtConfigUIFactory);
d->scan();
}
FcitxQtConfigUIFactory::~FcitxQtConfigUIFactory() {}
FcitxQtConfigUIWidget *FcitxQtConfigUIFactory::create(const QString &file) {
Q_D(FcitxQtConfigUIFactory);
auto path = normalizePath(file);
auto loader = d->plugins_.value(path);
if (!loader) {
return nullptr;
}
auto instance =
qobject_cast<FcitxQtConfigUIFactoryInterface *>(loader->instance());
if (!instance) {
return nullptr;
}
return instance->create(path.section('/', 1));
}
bool FcitxQtConfigUIFactory::test(const QString &file) {
Q_D(FcitxQtConfigUIFactory);
auto path = normalizePath(file);
return d->plugins_.contains(path);
}
void FcitxQtConfigUIFactoryPrivate::scan() {
fcitx::StandardPath::global().scanFiles(
fcitx::StandardPath::Type::Addon, "qt6",
[this](const std::string &path, const std::string &dirPath, bool user) {
do {
if (user) {
break;
}
QDir dir(QString::fromLocal8Bit(dirPath.c_str()));
QFileInfo fi(
dir.filePath(QString::fromLocal8Bit(path.c_str())));
QString filePath = fi.filePath(); // file name with path
QString fileName = fi.fileName(); // just file name
if (!QLibrary::isLibrary(filePath)) {
break;
}
QPluginLoader *loader = new QPluginLoader(filePath, this);
if (loader->metaData().value("IID") !=
QLatin1String(FcitxQtConfigUIFactoryInterface_iid)) {
delete loader;
break;
}
auto metadata = loader->metaData().value("MetaData").toObject();
auto files = metadata.value("files").toVariant().toStringList();
auto addon = metadata.value("addon").toVariant().toString();
for (const auto &file : files) {
plugins_[addon + "/" + file] = loader;
}
} while (0);
return true;
});
}
} // namespace fcitx

View File

@ -0,0 +1,55 @@
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_H_
#define _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_H_
#include <QMap>
#include <QObject>
#include <QStringList>
#include "fcitx5qt6widgetsaddons_export.h"
#include "fcitxqtconfiguiplugin.h"
#include "fcitxqtconfiguiwidget.h"
namespace fcitx {
class FcitxQtConfigUIFactoryPrivate;
/**
* ui plugin factory.
**/
class FCITX5QT6WIDGETSADDONS_EXPORT FcitxQtConfigUIFactory : public QObject {
Q_OBJECT
public:
/**
* create a plugin factory
*
* @param parent object parent
**/
explicit FcitxQtConfigUIFactory(QObject *parent = 0);
virtual ~FcitxQtConfigUIFactory();
/**
* create widget based on file name, it might return 0 if there is no match
*
* @param file file name need to be configured
* @return FcitxQtConfigUIWidget*
**/
FcitxQtConfigUIWidget *create(const QString &file);
/**
* a simplified version of create, but it just test if there is a valid
*entry or not
*
* @param file file name
* @return bool
**/
bool test(const QString &file);
private:
FcitxQtConfigUIFactoryPrivate *d_ptr;
Q_DECLARE_PRIVATE(FcitxQtConfigUIFactory);
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_H_

View File

@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_P_H_
#define _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_P_H_
#include "fcitxqtconfiguifactory.h"
#include <QObject>
#include <QPluginLoader>
#include <qpluginloader.h>
namespace fcitx {
class FcitxQtConfigUIFactoryPrivate : public QObject {
Q_OBJECT
public:
FcitxQtConfigUIFactoryPrivate(FcitxQtConfigUIFactory *conn);
virtual ~FcitxQtConfigUIFactoryPrivate();
FcitxQtConfigUIFactory *const q_ptr;
Q_DECLARE_PUBLIC(FcitxQtConfigUIFactory);
private:
void scan();
QMap<QString, QPluginLoader *> plugins_;
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTCONFIGUIFACTORY_P_H_

View File

@ -0,0 +1,15 @@
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "fcitxqtconfiguiplugin.h"
namespace fcitx {
FcitxQtConfigUIPlugin::FcitxQtConfigUIPlugin(QObject *parent)
: QObject(parent) {}
FcitxQtConfigUIPlugin::~FcitxQtConfigUIPlugin() {}
} // namespace fcitx

View File

@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef _WIDGETSADDONS_FCITXQTCONFIGUIPLUGIN_H_
#define _WIDGETSADDONS_FCITXQTCONFIGUIPLUGIN_H_
#include "fcitx5qt6widgetsaddons_export.h"
#include <QObject>
#include <QString>
#include <QStringList>
namespace fcitx {
class FcitxQtConfigUIWidget;
/**
* interface for qt config ui
*/
struct FCITX5QT6WIDGETSADDONS_EXPORT FcitxQtConfigUIFactoryInterface {
/**
* create new widget based on key
*
* @see FcitxQtConfigUIPlugin::files
*
* @return plugin name
*/
virtual FcitxQtConfigUIWidget *create(const QString &key) = 0;
};
#define FcitxQtConfigUIFactoryInterface_iid \
"org.fcitx.Fcitx.FcitxQtConfigUIFactoryInterface"
} // namespace fcitx
Q_DECLARE_INTERFACE(fcitx::FcitxQtConfigUIFactoryInterface,
FcitxQtConfigUIFactoryInterface_iid)
namespace fcitx {
/**
* base class for qt config ui
*/
class FCITX5QT6WIDGETSADDONS_EXPORT FcitxQtConfigUIPlugin
: public QObject,
public FcitxQtConfigUIFactoryInterface {
Q_OBJECT
Q_INTERFACES(fcitx::FcitxQtConfigUIFactoryInterface)
public:
explicit FcitxQtConfigUIPlugin(QObject *parent = 0);
virtual ~FcitxQtConfigUIPlugin();
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTCONFIGUIPLUGIN_H_

View File

@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "fcitxqtconfiguiwidget.h"
namespace fcitx {
FcitxQtConfigUIWidget::FcitxQtConfigUIWidget(QWidget *parent)
: QWidget(parent) {}
QString FcitxQtConfigUIWidget::icon() { return QLatin1String("fcitx"); }
bool FcitxQtConfigUIWidget::asyncSave() { return false; }
} // namespace fcitx

View File

@ -0,0 +1,81 @@
/*
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifndef _WIDGETSADDONS_FCITXQTCONFIGUIWIDGET_H_
#define _WIDGETSADDONS_FCITXQTCONFIGUIWIDGET_H_
#include "fcitx5qt6widgetsaddons_export.h"
#include <QtWidgets/QWidget>
namespace fcitx {
/**
* embedded gui for custom configuration
**/
class FCITX5QT6WIDGETSADDONS_EXPORT FcitxQtConfigUIWidget : public QWidget {
Q_OBJECT
public:
explicit FcitxQtConfigUIWidget(QWidget *parent = 0);
/**
* load the configuration, usually, this is being called upon a "reset"
*button clicked
* the outer gui will not call it for you for the first time, your
*initialization might
* want to call it by yourself.
*
* @return void
**/
virtual void load() = 0;
/**
* save the configuration
*
* @see asyncSave saveFinished
**/
virtual void save() = 0;
/**
* window title
*
* @return window title
**/
virtual QString title() = 0;
/**
* return the icon name of the window, see QIcon::fromTheme
*
* @return icon name
**/
virtual QString icon();
/**
* return the save function is async or not, default implementation is false
*
* @return bool
**/
virtual bool asyncSave();
Q_SIGNALS:
/**
* the configuration is changed or not, used to indicate parent gui
*
* @param changed is config changed
**/
void changed(bool changed);
/**
* if asyncSave return true, be sure to Q_EMIT this signal on all case
*
* @see asyncSave
**/
void saveFinished();
/// Save config for a specified path.
void saveSubConfig(const QString &path);
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTCONFIGUIWIDGET_H_

View File

@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#ifndef _WIDGETSADDONS_FCITXQTI18NHELPER_H_
#define _WIDGETSADDONS_FCITXQTI18NHELPER_H_
#include <QString>
#include <fcitx-utils/i18n.h>
namespace fcitx {
inline QString tr2fcitx(const char *message, const char *comment = nullptr) {
if (comment && comment[0] && message && message[0]) {
return QString(C_(comment, message));
} else if (message && message[0]) {
return QString(_(message));
} else {
return QString();
}
}
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTI18NHELPER_H_

View File

@ -0,0 +1,557 @@
/*
* SPDX-FileCopyrightText: 2013~2020 CSSlayer <wengxt@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/
/* this is forked from kdelibs/kdeui/kkeysequencewidget.cpp */
/*
Original Copyright header
SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org>
SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "fcitxqtkeysequencewidget.h"
#include "fcitxqtkeysequencewidget_p.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QHash>
#include <QKeyEvent>
#include <QLoggingCategory>
#include <QMessageBox>
#include <QTimer>
#include <QToolButton>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/key.h>
Q_LOGGING_CATEGORY(fcitx5qtKeysequenceWidget, "fcitx5.qt.keysequencewidget")
namespace fcitx {
namespace {
bool isX11LikePlatform() {
return QGuiApplication::platformName() == "xcb" ||
QGuiApplication::platformName().startsWith("wayland");
}
} // namespace
class FcitxQtKeySequenceWidgetPrivate {
public:
FcitxQtKeySequenceWidgetPrivate(FcitxQtKeySequenceWidget *q);
void init();
static bool isOkWhenModifierless(int keyQt);
void updateShortcutDisplay();
void startRecording();
void controlModifierlessTimout() {
if (keySequence_.size() != 0 && !modifierKeys_) {
// No modifier key pressed currently. Start the timout
modifierlessTimeout_.start(600);
} else {
// A modifier is pressed. Stop the timeout
modifierlessTimeout_.stop();
}
}
void cancelRecording() {
keySequence_ = oldKeySequence_;
doneRecording();
}
// private slot
void doneRecording();
// members
FcitxQtKeySequenceWidget *const q;
QHBoxLayout *layout_ = nullptr;
FcitxQtKeySequenceButton *keyButton_ = nullptr;
QToolButton *clearButton_ = nullptr;
QAction *keyCodeModeAction_ = nullptr;
QList<Key> keySequence_;
QList<Key> oldKeySequence_;
QTimer modifierlessTimeout_;
bool allowModifierless_ = false;
KeyStates modifierKeys_;
unsigned int qtModifierKeys_ = 0;
bool isRecording_ = false;
bool multiKeyShortcutsAllowed_ = false;
bool allowModifierOnly_ = false;
bool modifierAllowed_ = true;
bool keycodeAllowed_ = true;
};
FcitxQtKeySequenceWidgetPrivate::FcitxQtKeySequenceWidgetPrivate(
FcitxQtKeySequenceWidget *q)
: q(q) {}
FcitxQtKeySequenceWidget::FcitxQtKeySequenceWidget(QWidget *parent)
: QWidget(parent), d(new FcitxQtKeySequenceWidgetPrivate(this)) {
d->init();
setFocusProxy(d->keyButton_);
connect(d->keyButton_, &QPushButton::clicked, this,
&FcitxQtKeySequenceWidget::captureKeySequence);
connect(d->clearButton_, &QPushButton::clicked, this,
&FcitxQtKeySequenceWidget::clearKeySequence);
connect(&d->modifierlessTimeout_, &QTimer::timeout, this,
[this]() { d->doneRecording(); });
d->updateShortcutDisplay();
}
void FcitxQtKeySequenceWidgetPrivate::init() {
layout_ = new QHBoxLayout(q);
layout_->setContentsMargins(0, 0, 0, 0);
keyButton_ = new FcitxQtKeySequenceButton(this, q);
keyButton_->setFocusPolicy(Qt::StrongFocus);
keyButton_->setIcon(QIcon::fromTheme("configure"));
layout_->addWidget(keyButton_);
clearButton_ = new QToolButton(q);
layout_->addWidget(clearButton_);
keyCodeModeAction_ = new QAction(_("Key code mode"));
keyCodeModeAction_->setCheckable(true);
keyCodeModeAction_->setEnabled(isX11LikePlatform());
q->setContextMenuPolicy(Qt::ActionsContextMenu);
q->addAction(keyCodeModeAction_);
if (qApp->isLeftToRight())
clearButton_->setIcon(QIcon::fromTheme("edit-clear-locationbar-rtl"));
else
clearButton_->setIcon(QIcon::fromTheme("edit-clear-locationbar-ltr"));
q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
}
FcitxQtKeySequenceWidget::~FcitxQtKeySequenceWidget() { delete d; }
bool FcitxQtKeySequenceWidget::multiKeyShortcutsAllowed() const {
return d->multiKeyShortcutsAllowed_;
}
void FcitxQtKeySequenceWidget::setMultiKeyShortcutsAllowed(bool allowed) {
d->multiKeyShortcutsAllowed_ = allowed;
}
bool FcitxQtKeySequenceWidget::isModifierAllowed() const {
return d->modifierAllowed_;
}
void FcitxQtKeySequenceWidget::setModifierAllowed(bool allowed) {
d->modifierAllowed_ = allowed;
}
bool FcitxQtKeySequenceWidget::isKeycodeAllowed() const {
return d->keycodeAllowed_;
}
void FcitxQtKeySequenceWidget::setKeycodeAllowed(bool allowed) {
if (d->keycodeAllowed_ == allowed) {
return;
}
d->keycodeAllowed_ = allowed;
if (allowed) {
d->keyCodeModeAction_->setChecked(false);
addAction(d->keyCodeModeAction_);
} else {
removeAction(d->keyCodeModeAction_);
}
}
void FcitxQtKeySequenceWidget::setModifierlessAllowed(bool allow) {
d->allowModifierless_ = allow;
}
bool FcitxQtKeySequenceWidget::isModifierlessAllowed() {
return d->allowModifierless_;
}
bool FcitxQtKeySequenceWidget::isModifierOnlyAllowed() {
return d->allowModifierOnly_;
}
bool FcitxQtKeySequenceWidget::isModifierlessAllowed() const {
return d->allowModifierless_;
}
bool FcitxQtKeySequenceWidget::isModifierOnlyAllowed() const {
return d->allowModifierOnly_;
}
void FcitxQtKeySequenceWidget::setModifierOnlyAllowed(bool allow) {
d->allowModifierOnly_ = allow;
}
void FcitxQtKeySequenceWidget::setClearButtonShown(bool show) {
d->clearButton_->setVisible(show);
}
bool FcitxQtKeySequenceWidget::isClearButtonVisible() const {
return d->clearButton_->isVisible();
}
// slot
void FcitxQtKeySequenceWidget::captureKeySequence() { d->startRecording(); }
const QList<Key> &FcitxQtKeySequenceWidget::keySequence() const {
return d->keySequence_;
}
// slot
void FcitxQtKeySequenceWidget::setKeySequence(const QList<Key> &seq) {
// oldKeySequence holds the key sequence before recording started, if
// setKeySequence()
// is called while not recording then set oldKeySequence to the existing
// sequence so
// that the keySequenceChanged() signal is emitted if the new and previous
// key
// sequences are different
if (!d->isRecording_) {
d->oldKeySequence_ = d->keySequence_;
}
d->keySequence_ = QList<Key>();
for (auto key : seq) {
if (key.isValid()) {
d->keySequence_ << key;
}
}
d->doneRecording();
}
// slot
void FcitxQtKeySequenceWidget::clearKeySequence() {
setKeySequence(QList<Key>());
}
void FcitxQtKeySequenceWidgetPrivate::startRecording() {
modifierKeys_ = 0;
oldKeySequence_ = keySequence_;
keySequence_ = QList<Key>();
isRecording_ = true;
keyButton_->grabKeyboard();
if (!QWidget::keyboardGrabber()) {
qWarning() << "Failed to grab the keyboard! Most likely qt's nograb "
"option is active";
}
keyButton_->setDown(true);
updateShortcutDisplay();
}
void FcitxQtKeySequenceWidgetPrivate::doneRecording() {
modifierlessTimeout_.stop();
isRecording_ = false;
keyButton_->releaseKeyboard();
keyButton_->setDown(false);
if (keySequence_ == oldKeySequence_ && !allowModifierOnly_) {
// The sequence hasn't changed
updateShortcutDisplay();
return;
}
Q_EMIT q->keySequenceChanged(keySequence_);
updateShortcutDisplay();
}
void FcitxQtKeySequenceWidgetPrivate::updateShortcutDisplay() {
QString s = QString::fromUtf8(
Key::keyListToString(keySequence_, KeyStringFormat::Localized).c_str());
s.replace('&', QLatin1String("&&"));
if (isRecording_) {
if (modifierKeys_) {
if (!s.isEmpty())
s.append(",");
if (modifierKeys_ & KeyState::Super)
s += "Super+";
if (modifierKeys_ & KeyState::Ctrl)
s += "Control+";
if (modifierKeys_ & KeyState::Alt)
s += "Alt+";
if (modifierKeys_ & KeyState::Shift)
s += "Shift+";
if (modifierKeys_ & KeyState::Hyper)
s += "Hyper+";
} else if (keySequence_.size() == 0) {
s = "...";
}
// make it clear that input is still going on
s.append(" ...");
}
if (s.isEmpty()) {
s = _("Empty");
}
s.prepend(' ');
s.append(' ');
keyButton_->setText(s);
}
FcitxQtKeySequenceButton::~FcitxQtKeySequenceButton() {}
// prevent Qt from special casing Tab and Backtab
bool FcitxQtKeySequenceButton::event(QEvent *e) {
if (d->isRecording_ && e->type() == QEvent::KeyPress) {
keyPressEvent(static_cast<QKeyEvent *>(e));
return true;
}
// The shortcut 'alt+c' ( or any other dialog local action shortcut )
// ended the recording and triggered the action associated with the
// action. In case of 'alt+c' ending the dialog. It seems that those
// ShortcutOverride events get sent even if grabKeyboard() is active.
if (d->isRecording_ && e->type() == QEvent::ShortcutOverride) {
e->accept();
return true;
}
return QPushButton::event(e);
}
void FcitxQtKeySequenceButton::keyPressEvent(QKeyEvent *e) {
int keyQt = e->key();
if (keyQt == -1) {
// Qt sometimes returns garbage keycodes, I observed -1, if it doesn't
// know a key. We cannot do anything useful with those (several keys
// have -1, indistinguishable) and QKeySequence.toString() will also
// yield a garbage string.
QMessageBox::warning(
this, _("The key you just pressed is not supported by Qt."),
_("Unsupported Key"));
return d->cancelRecording();
}
// Same as Key::normalize();
unsigned int newQtModifiers =
e->modifiers() & (Qt::META | Qt::ALT | Qt::CTRL | Qt::SHIFT);
KeyStates newModifiers;
if (isX11LikePlatform()) {
newModifiers = KeyStates(e->nativeModifiers()) &
KeyStates{KeyState::Ctrl_Alt_Shift, KeyState::Hyper,
KeyState::Super};
newModifiers |=
Key::keySymToStates(static_cast<KeySym>(e->nativeVirtualKey()));
} else {
if (newQtModifiers & Qt::META) {
newModifiers |= KeyState::Super;
}
if (newQtModifiers & Qt::ALT) {
newModifiers |= KeyState::Alt;
}
if (newQtModifiers & Qt::CTRL) {
newModifiers |= KeyState::Ctrl;
}
if (newQtModifiers & Qt::SHIFT) {
newModifiers |= KeyState::Shift;
}
}
// don't have the return or space key appear as first key of the sequence
// when they
// were pressed to start editing - catch and them and imitate their effect
if (!d->isRecording_ &&
((keyQt == Qt::Key_Return || keyQt == Qt::Key_Space))) {
d->startRecording();
d->modifierKeys_ = newModifiers;
d->qtModifierKeys_ = newQtModifiers;
d->updateShortcutDisplay();
return;
}
// We get events even if recording isn't active.
if (!d->isRecording_)
return QPushButton::keyPressEvent(e);
e->accept();
d->modifierKeys_ = newModifiers;
d->qtModifierKeys_ = newQtModifiers;
switch (keyQt) {
case Qt::Key_AltGr: // or else we get unicode salad
return;
case Qt::Key_Shift:
case Qt::Key_Control:
case Qt::Key_Alt:
case Qt::Key_Super_L:
case Qt::Key_Super_R:
case Qt::Key_Hyper_L:
case Qt::Key_Hyper_R:
case Qt::Key_Meta:
case Qt::Key_Menu: // unused (yes, but why?)
d->controlModifierlessTimout();
d->updateShortcutDisplay();
break;
default:
// We now have a valid key press.
if (keyQt) {
if ((keyQt == Qt::Key_Backtab) &&
d->modifierKeys_.test(KeyState::Shift)) {
keyQt = Qt::Key_Tab | d->qtModifierKeys_;
} else {
keyQt |= d->qtModifierKeys_;
}
Key key;
if (d->keyCodeModeAction_->isChecked()) {
key = Key::fromKeyCode(e->nativeScanCode(), d->modifierKeys_);
} else {
if (isX11LikePlatform()) {
key = Key(static_cast<KeySym>(e->nativeVirtualKey()),
KeyStates(e->nativeModifiers()))
.normalize();
} else {
qCWarning(fcitx5qtKeysequenceWidget)
<< "Unsupported platform";
}
}
// Check the first key.
if (d->keySequence_.size() == 0) {
if (!d->allowModifierless_ && key.states() == 0) {
return;
}
if (!d->modifierAllowed_ && key.states() != 0) {
return;
}
}
if (key.isValid()) {
d->keySequence_ << key;
}
if ((!d->multiKeyShortcutsAllowed_) ||
(d->keySequence_.size() >= 4)) {
d->doneRecording();
return;
}
d->controlModifierlessTimout();
d->updateShortcutDisplay();
}
}
}
void FcitxQtKeySequenceButton::keyReleaseEvent(QKeyEvent *e) {
if (e->key() == -1) {
// ignore garbage, see keyPressEvent()
return;
}
if (!d->isRecording_)
return QPushButton::keyReleaseEvent(e);
e->accept();
if (!d->multiKeyShortcutsAllowed_ && d->allowModifierOnly_ &&
(e->key() == Qt::Key_Shift || e->key() == Qt::Key_Control ||
e->key() == Qt::Key_Meta || e->key() == Qt::Key_Alt)) {
Key key;
if (isX11LikePlatform()) {
if (d->keyCodeModeAction_->isChecked()) {
key = Key::fromKeyCode(e->nativeScanCode(), key.states());
} else {
key = Key(static_cast<KeySym>(e->nativeVirtualKey()),
KeyStates(e->nativeModifiers()))
.normalize();
}
d->keySequence_ = QList<Key>({key});
}
d->doneRecording();
return;
}
unsigned int newQtModifiers =
e->modifiers() & (Qt::META | Qt::ALT | Qt::CTRL | Qt::SHIFT);
KeyStates newModifiers;
if (isX11LikePlatform()) {
newModifiers = KeyStates(e->nativeModifiers()) &
KeyStates{KeyState::Ctrl_Alt_Shift, KeyState::Hyper,
KeyState::Super};
newModifiers &=
~Key::keySymToStates(static_cast<KeySym>(e->nativeVirtualKey()));
} else {
if (newQtModifiers & Qt::META) {
newModifiers |= KeyState::Super;
}
if (newQtModifiers & Qt::ALT) {
newModifiers |= KeyState::Alt;
}
if (newQtModifiers & Qt::CTRL) {
newModifiers |= KeyState::Ctrl;
}
if (newQtModifiers & Qt::SHIFT) {
newModifiers |= KeyState::Shift;
}
}
// if a modifier that belongs to the shortcut was released...
if ((newModifiers & d->modifierKeys_) < d->modifierKeys_) {
d->modifierKeys_ = newModifiers;
d->controlModifierlessTimout();
d->updateShortcutDisplay();
}
}
// static
bool FcitxQtKeySequenceWidgetPrivate::isOkWhenModifierless(int keyQt) {
// this whole function is a hack, but especially the first line of code
if (QKeySequence(keyQt).toString().length() == 1)
return false;
switch (keyQt) {
case Qt::Key_Return:
case Qt::Key_Space:
case Qt::Key_Tab:
case Qt::Key_Backtab: // does this ever happen?
case Qt::Key_Backspace:
case Qt::Key_Delete:
return false;
default:
return true;
}
}
} // namespace fcitx
#include "moc_fcitxqtkeysequencewidget.cpp"

View File

@ -0,0 +1,152 @@
/*
* SPDX-FileCopyrightText: 2013~2017 CSSlayer <wengxt@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/
#ifndef _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_H_
#define _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_H_
/* this is forked from kdelibs/kdeui/kkeysequencewidget.h */
/*
Original Copyright header
This file is part of the KDE libraries
SPDX-FileCopyrightText: 2001, 2002 Ellis Whitehead <ellis@kde.org>
SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QList>
#include <QPushButton>
#include <fcitx-utils/key.h>
#include "fcitx5qt6widgetsaddons_export.h"
namespace fcitx {
class FcitxQtKeySequenceWidgetPrivate;
class FCITX5QT6WIDGETSADDONS_EXPORT FcitxQtKeySequenceWidget : public QWidget {
Q_OBJECT
Q_PROPERTY(bool multiKeyShortcutsAllowed READ multiKeyShortcutsAllowed WRITE
setMultiKeyShortcutsAllowed)
Q_PROPERTY(bool modifierlessAllowed READ isModifierlessAllowed WRITE
setModifierlessAllowed)
Q_PROPERTY(
bool modifierAllowed READ isModifierAllowed WRITE setModifierAllowed)
Q_PROPERTY(
bool keycodeAllowed READ isKeycodeAllowed WRITE setKeycodeAllowed)
Q_PROPERTY(bool modifierOnlyAllowed READ isModifierOnlyAllowed WRITE
setModifierOnlyAllowed)
public:
/**
* Constructor.
*/
explicit FcitxQtKeySequenceWidget(QWidget *parent = 0);
/**
* Destructs the widget.
*/
virtual ~FcitxQtKeySequenceWidget();
/**
* @brief Set whether allow multiple shortcuts.
*
* @param allow
*/
void setMultiKeyShortcutsAllowed(bool allow);
bool multiKeyShortcutsAllowed() const;
/**
* @brief Set whether allow modifier less that produce text, such as just
* key A.
*
* @param allow
*/
void setModifierlessAllowed(bool allow);
// FIXME: remove this
bool isModifierlessAllowed();
bool isModifierlessAllowed() const;
/**
* @brief Set whether allow key that has modifier.
*
* @param allow
* @since 5.0.12
*/
void setModifierAllowed(bool allow);
bool isModifierAllowed() const;
/**
* @brief Set whether allow key to use key code.
*
* @param allow
* @since 5.0.12
*/
void setKeycodeAllowed(bool allow);
bool isKeycodeAllowed() const;
/**
* @brief Set whether allow modifier only key, such as only left control.
*
* @param allow allow modifier only key to be captured.
*/
void setModifierOnlyAllowed(bool allow);
// FIXME: remove this
bool isModifierOnlyAllowed();
bool isModifierOnlyAllowed() const;
void setClearButtonShown(bool show);
bool isClearButtonVisible() const;
const QList<Key> &keySequence() const;
Q_SIGNALS:
void keySequenceChanged(const QList<Key> &seq);
public Q_SLOTS:
void captureKeySequence();
void setKeySequence(const QList<Key> &seq);
void clearKeySequence();
private:
friend class FcitxQtKeySequenceWidgetPrivate;
FcitxQtKeySequenceWidgetPrivate *const d;
Q_DISABLE_COPY(FcitxQtKeySequenceWidget)
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_H_

View File

@ -0,0 +1,75 @@
/*
* SPDX-FileCopyrightText: 2012~2017 CSSlayer <wengxt@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/
#ifndef _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_P_H_
#define _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_P_H_
/* this is forked from kdelibs/kdeui/kkeysequencewidget_p.h */
/*
Original Copyright header
SPDX-FileCopyrightText: 2001, 2002 Ellis Whitehead <ellis@kde.org>
SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QAction>
#include <QHBoxLayout>
#include <QPushButton>
#include <QToolButton>
namespace fcitx {
class FcitxQtKeySequenceWidgetPrivate;
class FcitxQtKeySequenceButton : public QPushButton {
Q_OBJECT
public:
explicit FcitxQtKeySequenceButton(FcitxQtKeySequenceWidgetPrivate *d,
QWidget *parent)
: QPushButton(parent), d(d) {}
virtual ~FcitxQtKeySequenceButton();
protected:
/**
* Reimplemented for internal reasons.
*/
bool event(QEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
private:
FcitxQtKeySequenceWidgetPrivate *const d;
};
} // namespace fcitx
#endif // _WIDGETSADDONS_FCITXQTKEYSEQUENCEWIDGET_P_H_