fixed filepaths and removed all cmake files
This commit is contained in:
@@ -68,6 +68,47 @@ DatabaseManager::~DatabaseManager()
|
||||
}
|
||||
}
|
||||
|
||||
bool DatabaseManager::createDatabase(const QString &dbPath)
|
||||
{
|
||||
// Use a dedicated connection name so it doesn't conflict with the main one
|
||||
const QString connName = "db_create";
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connName);
|
||||
db.setDatabaseName(dbPath);
|
||||
|
||||
if (!db.open()) {
|
||||
qDebug() << "createDatabase: failed to open/create file:" << db.lastError().text();
|
||||
QSqlDatabase::removeDatabase(connName);
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlQuery q(db);
|
||||
bool ok = q.exec(
|
||||
"CREATE TABLE IF NOT EXISTS ocr_results ("
|
||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
" filename TEXT UNIQUE,"
|
||||
" full_path TEXT,"
|
||||
" ocr_text TEXT,"
|
||||
" file_size INTEGER,"
|
||||
" created_date TEXT,"
|
||||
" ocr_date TEXT"
|
||||
");"
|
||||
);
|
||||
|
||||
if (!ok) {
|
||||
qDebug() << "createDatabase: failed to create schema:" << q.lastError().text();
|
||||
db.close();
|
||||
QSqlDatabase::removeDatabase(connName);
|
||||
return false;
|
||||
}
|
||||
|
||||
db.close();
|
||||
}
|
||||
QSqlDatabase::removeDatabase(connName);
|
||||
qDebug() << "createDatabase: new database created at" << dbPath;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DatabaseManager::initialize(const QString &dbPath)
|
||||
{
|
||||
// Check if database is already initialized
|
||||
|
||||
@@ -55,6 +55,14 @@ public:
|
||||
*/
|
||||
bool initialize(const QString &dbPath);
|
||||
|
||||
/**
|
||||
* @brief Create a new empty database with the required schema at the given path.
|
||||
* The parent directory must already exist.
|
||||
* @param dbPath Path where the SQLite database should be created
|
||||
* @return True if creation was successful, false otherwise
|
||||
*/
|
||||
static bool createDatabase(const QString &dbPath);
|
||||
|
||||
/**
|
||||
* @brief Get all images from the database with pagination
|
||||
* @param offset Starting position (0-based) for pagination
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ int main(int argc, char *argv[])
|
||||
QApplication app(argc, argv);
|
||||
|
||||
// Set application properties
|
||||
app.setApplicationName("Screenshot Gallery");
|
||||
app.setApplicationName("screenshot-gallery");
|
||||
app.setApplicationVersion("1.0.0");
|
||||
|
||||
// Set application icon
|
||||
|
||||
+73
-10
@@ -13,6 +13,7 @@
|
||||
#include <QFileDialog>
|
||||
#include <QDesktopServices>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <stdexcept>
|
||||
|
||||
// Define settings file path
|
||||
@@ -59,6 +60,9 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
|
||||
// Set up UI
|
||||
createLayout();
|
||||
|
||||
// Apply loaded preload count to the gallery (createLayout uses the hardcoded default)
|
||||
m_imageGallery->setPageSize(m_imagePreloadCount);
|
||||
|
||||
// Initialize typing inactivity timer
|
||||
m_typingTimer->setSingleShot(true);
|
||||
@@ -158,7 +162,13 @@ void MainWindow::createLayout()
|
||||
QAction *settingsAction = new QAction(tr("&Settings"), this);
|
||||
connect(settingsAction, &QAction::triggered, this, &MainWindow::handleSettings);
|
||||
fileMenu->addAction(settingsAction);
|
||||
|
||||
|
||||
// Add Update Database action
|
||||
QAction *updateDbAction = new QAction(tr("&Update Database"), this);
|
||||
updateDbAction->setToolTip(tr("Run OCR on new screenshots and update the database"));
|
||||
connect(updateDbAction, &QAction::triggered, this, &MainWindow::handleUpdateDatabase);
|
||||
fileMenu->addAction(updateDbAction);
|
||||
|
||||
// Add separator
|
||||
fileMenu->addSeparator();
|
||||
|
||||
@@ -187,15 +197,31 @@ void MainWindow::createLayout()
|
||||
|
||||
void MainWindow::initializeDatabase()
|
||||
{
|
||||
// Check if database file exists
|
||||
// Ensure the parent directory exists
|
||||
QFileInfo dbFileInfo(m_databasePath);
|
||||
QDir dbDir = dbFileInfo.absoluteDir();
|
||||
if (!dbDir.exists()) {
|
||||
if (!dbDir.mkpath(".")) {
|
||||
QString errorMsg = tr("Could not create database directory: %1").arg(dbDir.absolutePath());
|
||||
statusBar()->showMessage(errorMsg, 10000);
|
||||
QMessageBox::critical(this, tr("Database Error"), errorMsg);
|
||||
m_hasValidDatabase = false;
|
||||
return;
|
||||
}
|
||||
qDebug() << "Created database directory:" << dbDir.absolutePath();
|
||||
}
|
||||
|
||||
// Create the database with the required schema if it does not exist yet
|
||||
if (!dbFileInfo.exists() || !dbFileInfo.isFile()) {
|
||||
QString errorMsg = tr("Database file not found: %1").arg(m_databasePath);
|
||||
statusBar()->showMessage(errorMsg, 10000);
|
||||
QMessageBox::critical(this, tr("Database Error"), errorMsg);
|
||||
qDebug() << "Database file not found:" << m_databasePath;
|
||||
m_hasValidDatabase = false;
|
||||
return;
|
||||
qDebug() << "Database not found – creating new database at:" << m_databasePath;
|
||||
if (!DatabaseManager::createDatabase(m_databasePath)) {
|
||||
QString errorMsg = tr("Could not create database: %1").arg(m_databasePath);
|
||||
statusBar()->showMessage(errorMsg, 10000);
|
||||
QMessageBox::critical(this, tr("Database Error"), errorMsg);
|
||||
m_hasValidDatabase = false;
|
||||
return;
|
||||
}
|
||||
statusBar()->showMessage(tr("Created new database at %1").arg(m_databasePath), 5000);
|
||||
}
|
||||
|
||||
// Try to initialize database
|
||||
@@ -386,6 +412,41 @@ void MainWindow::handleSettings()
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::handleUpdateDatabase()
|
||||
{
|
||||
// If a dialog is already open, just bring it to the front
|
||||
if (m_updateDialog) {
|
||||
m_updateDialog->raise();
|
||||
m_updateDialog->activateWindow();
|
||||
return;
|
||||
}
|
||||
|
||||
// Locate the OCR script next to the application binary
|
||||
const QString appDir = QCoreApplication::applicationDirPath();
|
||||
// Try <appDir>/../OCR-scripts/ (dev tree) then <appDir>/OCR-scripts/ (installed)
|
||||
QString scriptPath = appDir + "/../OCR-scripts/ocr_screenshots.py";
|
||||
if (!QFileInfo::exists(scriptPath))
|
||||
scriptPath = appDir + "/OCR-scripts/ocr_screenshots.py";
|
||||
|
||||
m_updateDialog = new UpdateDatabaseDialog(scriptPath, m_databasePath, m_screenshotsDir, this);
|
||||
|
||||
// Clean up pointer when the dialog is closed
|
||||
connect(m_updateDialog, &QDialog::finished, this, [this]() {
|
||||
m_updateDialog->deleteLater();
|
||||
m_updateDialog = nullptr;
|
||||
});
|
||||
|
||||
// Reload the gallery automatically when OCR finishes
|
||||
connect(m_updateDialog, &UpdateDatabaseDialog::updateFinished, this, [this]() {
|
||||
statusBar()->showMessage(tr("Database updated – reloading gallery…"), 3000);
|
||||
// The DB connection is already open; just refresh the gallery
|
||||
if (m_hasValidDatabase)
|
||||
displayAllImages();
|
||||
});
|
||||
|
||||
m_updateDialog->show();
|
||||
}
|
||||
|
||||
void MainWindow::loadSettings()
|
||||
{
|
||||
// Ensure config directory exists
|
||||
@@ -397,8 +458,10 @@ void MainWindow::loadSettings()
|
||||
|
||||
QSettings settings(CONFIG_FILE_PATH, QSettings::IniFormat);
|
||||
|
||||
// Load database path with fallback to default
|
||||
m_databasePath = settings.value("databasePath", "/home/master/screenshot_ocr.db").toString();
|
||||
// Load database path with fallback to XDG data dir default
|
||||
const QString defaultDbPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
|
||||
+ "/screenshot_ocr.db";
|
||||
m_databasePath = settings.value("databasePath", defaultDbPath).toString();
|
||||
|
||||
// Load screenshots directory with fallback to home/Screenshots
|
||||
m_screenshotsDir = settings.value("screenshotsDir", QDir::homePath() + "/Screenshots").toString();
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <QDebug>
|
||||
#include "databasemanager.h"
|
||||
#include "imagegallery.h"
|
||||
#include "updatedatabasedialog.h"
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
@@ -30,6 +31,7 @@ private slots:
|
||||
void handleOpenFile();
|
||||
void handleOpenFileWith();
|
||||
void handleSettings();
|
||||
void handleUpdateDatabase();
|
||||
void applySettings();
|
||||
void loadSettings();
|
||||
|
||||
@@ -58,6 +60,9 @@ private:
|
||||
QString m_screenshotsDir;
|
||||
int m_imagePreloadCount;
|
||||
|
||||
// OCR update dialog (non-modal – keep pointer so it stays alive)
|
||||
UpdateDatabaseDialog *m_updateDialog = nullptr;
|
||||
|
||||
// Settings file path
|
||||
static const QString CONFIG_FILE_PATH;
|
||||
|
||||
|
||||
@@ -200,7 +200,9 @@ void SettingsDialog::loadSettings()
|
||||
m_screenshotsDirEdit->setText(screenshotsDir);
|
||||
|
||||
// Load database path
|
||||
QString databasePath = settings.value("databasePath", QDir::homePath() + "/" + DEFAULT_DATABASE_FILENAME).toString();
|
||||
const QString defaultDbPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
|
||||
+ "/" + DEFAULT_DATABASE_FILENAME;
|
||||
QString databasePath = settings.value("databasePath", defaultDbPath).toString();
|
||||
m_databasePathEdit->setText(databasePath);
|
||||
|
||||
// Load preload count
|
||||
|
||||
Reference in New Issue
Block a user