Skip to content

Commit

Permalink
Merge pull request #355 from NabilSnigdho/develop
Browse files Browse the repository at this point in the history
feat: add option to import OBK *.json layout file
  • Loading branch information
mominul authored Nov 5, 2023
2 parents 780bd40 + 262eab0 commit 7586ea9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/frontend/LayoutConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,22 @@ ConversionResult LayoutConverter::convertAvroLayout(QString path) {
return saveLayout(layout, savePath);
}

ConversionResult LayoutConverter::saveLayout(QString path) {
QFile jsonFile(path);
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
return OpenError;
}
QString version = QJsonDocument::fromJson(jsonFile.readAll()).object()
.value("info").toObject()
.value("version").toString();
if (version == "1") return convertLayoutFormat(path);
if (version != "2") return UnsupportedLayout;

QFileInfo fileInfo(jsonFile);
QString savePath = folders.getUserLayoutPath() + fileInfo.fileName();
return jsonFile.copy(savePath) ? Ok : SaveError;
}

ConversionResult LayoutConverter::saveLayout(QJsonObject obj, QString path) {
QFile saveFile(path);
if (!saveFile.open(QIODevice::WriteOnly)) {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/LayoutConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ class LayoutConverter {
public:
ConversionResult convertAvroLayout(QString path);
ConversionResult convertLayoutFormat(QString path);
ConversionResult saveLayout(QString path);
};
36 changes: 28 additions & 8 deletions src/frontend/TopBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
static const QString DEFS_URL = "https://raw.githubusercontent.com/OpenBangla/OpenBangla-Keyboard/master/UPDATES.json";

TopBar::TopBar(bool darkIcon, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TopBar) {
QMainWindow(parent),
ui(new Ui::TopBar) {
ui->setupUi(this);

gLayout = new Layout();
Expand Down Expand Up @@ -86,7 +86,7 @@ TopBar::TopBar(bool darkIcon, QWidget *parent) :
// Update the counter to show only the message for the first three times
gSettings->setTrayInfoCount(count + 1);
}

#ifndef NO_UPDATE_CHECK
updater = QSimpleUpdater::getInstance();

Expand Down Expand Up @@ -115,7 +115,7 @@ void TopBar::SetupTopBar() {
if (gSettings->getTopBarWindowPosition() == QPoint(0, 0)) {
int width = this->frameGeometry().width();
int height = this->frameGeometry().height();

QApplication *app = (QApplication *) QApplication::instance();
QScreen *screen = app->primaryScreen();

Expand Down Expand Up @@ -214,7 +214,7 @@ void TopBar::SetupTrayIcon() {
gSettings->getTopBarVisibility() ? "Hide the TopBar" : "Show the TopBar",
this
);

connect(trayTopBarVisibility, &QAction::triggered, [&]() {
if(this->isVisible()) {
this->setVisible(false);
Expand All @@ -236,7 +236,7 @@ void TopBar::SetupTrayIcon() {
trayMenu->addSeparator();
trayMenu->addAction(trayTopBarVisibility);
trayMenu->addAction(trayQuit);

tray->setContextMenu(trayMenu);
tray->setVisible(true);
}
Expand Down Expand Up @@ -293,9 +293,9 @@ void TopBar::layoutMenuLayouts_clicked() {

void TopBar::layoutMenuInstall_clicked() {
QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, "Select Keyboard Layout", QDir::homePath(),
"Avro Keyboard 5 Keyboard Layout (*.avrolayout)");
"All Supported Layouts (*.avrolayout *.json);;Avro Keyboard 5 Keyboard Layout (*.avrolayout);;OpenBangla Keyboard Layout (*.json)");
LayoutConverter conv;
if (fileName.contains(".avrolayout") && fileName != "") {
if (fileName.endsWith(".avrolayout")) {
ConversionResult res = conv.convertAvroLayout(fileName);
switch (res) {
case Ok:
Expand All @@ -317,6 +317,26 @@ void TopBar::layoutMenuInstall_clicked() {
QMessageBox::Ok);
break;
}
} else if (fileName.endsWith(".json")) {
ConversionResult res = conv.saveLayout(fileName);
switch (res) {
case Ok:
QMessageBox::information(Q_NULLPTR, "OpenBangla Keyboard", "Layout Installed Successfully",
QMessageBox::Ok);
break;
case UnsupportedLayout:
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Unsupported Layout file!",
QMessageBox::Ok);
break;
case OpenError:
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard",
"An error occurred while opening the layout file!", QMessageBox::Ok);
break;
case SaveError:
QMessageBox::critical(Q_NULLPTR, "OpenBangla Keyboard", "Error occurred while saving the file!",
QMessageBox::Ok);
break;
}
}
RefreshLayouts();
}
Expand Down

0 comments on commit 7586ea9

Please sign in to comment.