Skip to content

Commit

Permalink
Add lookup command for the Qt interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kovzol committed Feb 24, 2024
1 parent ffc3106 commit a409a75
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 31 deletions.
18 changes: 9 additions & 9 deletions fingerprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ using namespace std;

Fingerprint getFingerprint(string text) {
Fingerprint f;
for (int i=0; i<N; ++i)
for (int j=0; j<N; ++j) {
for (int i=0; i<N_GREEK_LETTERS; ++i)
for (int j=0; j<N_GREEK_LETTERS; ++j) {
f.m_data[i][j] = 0;
}
for (int i=0; i<text.length() - 1; ++i) {
Expand Down Expand Up @@ -40,21 +40,21 @@ Fingerprint getFingerprint(Book b, int start, int length) {

int dist(Fingerprint f1, Fingerprint f2) {
int d = 0;
for (int i=0; i<N; ++i)
for (int j=0; j<N; ++j) {
for (int i=0; i<N_GREEK_LETTERS; ++i)
for (int j=0; j<N_GREEK_LETTERS; ++j) {
d += abs(f1.m_data[i][j]-f2.m_data[i][j]);
}
return d;
}

void printDist(Fingerprint f1, Fingerprint f2) {
cout << " ";
for (int j=0; j<N; ++j)
for (int j=0; j<N_GREEK_LETTERS; ++j)
printf("%c", j + 'a');
cout << endl;
for (int i=0; i<N; ++i) {
for (int i=0; i<N_GREEK_LETTERS; ++i) {
printf("%c", i + 'a');
for (int j=0; j<N; ++j) {
for (int j=0; j<N_GREEK_LETTERS; ++j) {
if (f1.m_data[i][j] == 0 && f2.m_data[i][j] == 0)
cout << " ";
else
Expand All @@ -74,8 +74,8 @@ double jaccard_dist(const string& text1, const string& text2) {
int d = 0;
Fingerprint f1 = getFingerprint(text1);
Fingerprint f2 = getFingerprint(text2);
for (int i=0; i<N; ++i)
for (int j=0; j<N; ++j) {
for (int i=0; i<N_GREEK_LETTERS; ++i)
for (int j=0; j<N_GREEK_LETTERS; ++j) {
d1 += f1.m_data[i][j];
d2 += f2.m_data[i][j];
d += min(f1.m_data[i][j], f2.m_data[i][j]);
Expand Down
4 changes: 2 additions & 2 deletions fingerprint.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef FINGERPRINT_H
#define FINGERPRINT_H

#define N 25
#define N_GREEK_LETTERS 25

#include "book.h"

typedef struct Fingerprint {
unsigned short int m_data [N][N];
unsigned short int m_data [N_GREEK_LETTERS][N_GREEK_LETTERS];
} Fingerprint;

Fingerprint getFingerprint(Book book, int start, int length);
Expand Down
116 changes: 99 additions & 17 deletions qt/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <QtWidgets>

#include "mainwindow.h"

#include "books.h"
#include "cli.h"
#include "swmgr.h"

#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>

#include <QCoreApplication>
#include <qtconcurrentrun.h>
#include <QThread>
Expand All @@ -20,6 +21,9 @@ using namespace sword;
extern bool booksAdded;
extern string text[2];
extern vector<bool> textset;
extern string collect_info;

string lookupText = "LXX Genesis 1:1"; // example

QString getClipboardInfos() {
QString intro = "<b>Contents of the clipboards in Greek (and in a-y notation)</b>";
Expand Down Expand Up @@ -48,19 +52,24 @@ MainWindow::MainWindow()
QWidget *topFiller = new QWidget;
topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

infoLabel = new QLabel(getClipboardInfos());
infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
infoLabel->setAlignment(Qt::AlignCenter);
infoLabel->setWordWrap(true);
clipboardInfos = new QLabel(getClipboardInfos());
clipboardInfos->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
clipboardInfos->setAlignment(Qt::AlignCenter);
clipboardInfos->setWordWrap(true);

QWidget *bottomFiller = new QWidget;
bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

passageInfos = new QTextEdit;
passageInfos->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(5, 5, 5, 5);
layout->addWidget(topFiller);
layout->addWidget(infoLabel);
layout->addWidget(clipboardInfos);
layout->addWidget(passageInfos);
layout->addWidget(bottomFiller);

widget->setLayout(layout);
createActions();
createMenus();
Expand All @@ -87,10 +96,10 @@ void MainWindow::addBibles()
QFuture<void> future = QtConcurrent::run(addBiblesThread, statusBar());
}

void MainWindow::greekText(int index)
void MainWindow::greekTextN(int index)
{
QInputDialog inputDialog(this);
inputDialog.setWindowTitle(tr("Define a Greek text and put its Latin transcription in clipboard"));
inputDialog.setWindowTitle(tr("Text " + index));
inputDialog.setLabelText(tr("Greek text:"));
if (textset[index]) {
inputDialog.setTextValue(latinToGreek(text[index]).c_str());
Expand All @@ -117,23 +126,23 @@ void MainWindow::greekText(int index)
textset[index] = true; // activate clipboard
info("Stored internally as " + processed + "."); // Success!

infoLabel->setText(getClipboardInfos());
clipboardInfos->setText(getClipboardInfos());
}

void MainWindow::greekText1()
{
this->greekText(0);
this->greekTextN(0);
}

void MainWindow::greekText2()
{
this->greekText(1);
this->greekTextN(1);
}

void MainWindow::latinText(int index)
void MainWindow::latinTextN(int index)
{
QInputDialog inputDialog(this);
inputDialog.setWindowTitle(tr("Put a Latin (a-y) transcription in clipboard"));
inputDialog.setWindowTitle(tr("Latin text " + index));
inputDialog.setLabelText(tr("Latin text:"));
if (textset[index]) {
inputDialog.setTextValue(text[index].c_str());
Expand All @@ -149,19 +158,72 @@ void MainWindow::latinText(int index)
textset[index] = true; // activate clipboard
info("Stored."); // Success!

infoLabel->setText(getClipboardInfos());
clipboardInfos->setText(getClipboardInfos());
}

void MainWindow::latinText1()
{
this->latinText(0);
this->latinTextN(0);
}

void MainWindow::latinText2()
{
this->latinText(1);
this->latinTextN(1);
}

void MainWindow::lookup()
{
QInputDialog inputDialog(this);
inputDialog.setWindowTitle(tr("Lookup"));
inputDialog.setLabelText(tr("Verse:"));
inputDialog.setTextValue(lookupText.c_str());
if (inputDialog.exec() != QDialog::Accepted)
return;
const QString value = inputDialog.textValue().trimmed();
if (value.isEmpty())
return;
lookupText = value.toStdString();
string rest = lookupText;

// Mostly taken from cli:
vector<string> tokens;
boost::split(tokens, rest, boost::is_any_of(" "));
int restSize = tokens.size();
if (restSize == 3) { // e.g. lookup LXX Genesis 1:1
collect_info = ""; // reset communication buffer
lookupTranslation(tokens[0], tokens[1], tokens[2]); // simple lookup via Sword
passageInfos->append(("<b>" + rest + "</b>" + "<br>" + collect_info).c_str());

QTextCursor tc = passageInfos->textCursor();
tc.setPosition(passageInfos->document()->characterCount() - 1);
passageInfos->setTextCursor(tc); // Move cursor to the end.
} else {
// QErrorMessage m;
// m.showMessage("Invalid input.");
// m.exec();
QString message = "Invalid input (3 words are needed: Bible edition, book, chapter:verse).";
statusBar()->showMessage(message);
}
return; // Success!
}

void MainWindow::lookupN(int n)
{

}

void MainWindow::lookup1()
{
this->lookupN(1);
}

void MainWindow::lookup2()
{
this->lookupN(2);
}




void MainWindow::about()
{
Expand Down Expand Up @@ -204,6 +266,20 @@ void MainWindow::createActions()
latinText2Act->setStatusTip(tr("Put a Latin (a-y) transcription in clipboard 2"));
connect(latinText2Act, &QAction::triggered, this, &MainWindow::latinText2);

lookupAct = new QAction(tr("&Lookup"), this);
lookupAct->setStatusTip(tr("Search for a verse in a book in the given Bible"));
connect(lookupAct, &QAction::triggered, this, &MainWindow::lookup);

lookup1Act = new QAction(tr("Lookup &1"), this);
lookup1Act->setStatusTip(tr("Search for a passage in a book in the given Bible and put it in clipboard 1"));
connect(lookup1Act, &QAction::triggered, this, &MainWindow::lookup1);
lookup1Act->setDisabled(true);

lookup2Act = new QAction(tr("Lookup &2"), this);
lookup2Act->setStatusTip(tr("Search for a passage in a book in the given Bible and put it in clipboard 1"));
connect(lookup2Act, &QAction::triggered, this, &MainWindow::lookup2);
lookup2Act->setDisabled(true);

aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
Expand All @@ -227,6 +303,12 @@ void MainWindow::createMenus()
editMenu->addAction(latinText1Act);
editMenu->addAction(latinText2Act);

passageMenu = menuBar()->addMenu(tr("&Passage"));
passageMenu->addAction(lookupAct);
passageMenu->addSeparator();
passageMenu->addAction(lookup1Act);
passageMenu->addAction(lookup2Act);

helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
Expand Down
16 changes: 13 additions & 3 deletions qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWidgets>

QT_BEGIN_NAMESPACE
class QAction;
Expand All @@ -25,27 +26,36 @@ private slots:
void greekText2();
void latinText1();
void latinText2();
void lookup();
void lookup1();
void lookup2();
void about();
void aboutQt();

private:
void createActions();
void createMenus();
void greekText(int n);
void latinText(int n);
void greekTextN(int n);
void latinTextN(int n);
void lookupN(int n);

QMenu *fileMenu;
QMenu *editMenu;
QMenu *passageMenu;
QMenu *helpMenu;
QAction *addBiblesAct;
QAction *exitAct;
QAction *greekText1Act;
QAction *greekText2Act;
QAction *latinText1Act;
QAction *latinText2Act;
QAction *lookupAct;
QAction *lookup1Act;
QAction *lookup2Act;
QAction *aboutAct;
QAction *aboutQtAct;
QLabel *infoLabel;
QLabel *clipboardInfos;
QTextEdit *passageInfos;
};

#endif

0 comments on commit a409a75

Please sign in to comment.