Skip to content

Commit

Permalink
Small code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nighty3098 committed Sep 2, 2024
1 parent 905e943 commit 9d1ca1e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 46 deletions.
73 changes: 43 additions & 30 deletions src/CodeKeeper/keeperFunc/projectsFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,30 @@ void MainWindow::activateProjectContextMenu(const QPoint &pos, QListWidget *list

void MainWindow::onMovingProjectFrom(QListWidgetItem *item, QListWidget *list)
{
qDebug() << "Moving project: " << item->text() << " from: " << list->objectName();
if (item && list)
{
qDebug() << "Moving project: " << item->text() << " from: " << list->objectName();
}
}

void MainWindow::onMovingProjectTo(QListWidgetItem *item, QListWidget *list)
{
qDebug() << "Moved project: " << item->text() << " to: " << list->objectName();
QStringList data = item->text().split("\n");
QString status = list->objectName();
QString date = getCurrentDateTimeString();
if (item && list)
{
qDebug() << "Moved project: " << item->text() << " to: " << list->objectName();
QStringList data = item->text().split("\n");
if (data.size() >= 3) // Check if data has at least 3 elements
{
QString status = list->objectName();
QString date = getCurrentDateTimeString();

updateProjectStatus(&status, &date, &data[2]);
updateProjectStatus(&status, &date, &data[2]);
}
else
{
qWarning() << "Invalid project data format";
}
}
}

void MainWindow::createProject()
Expand All @@ -87,7 +100,7 @@ void MainWindow::createProject()

void MainWindow::removeProject()
{
QListWidget *listWidgets[] = {notStartedProjects, startedProjects, finishlineProjects, finishedProjects};
QList<QListWidget *> listWidgets = {notStartedProjects, startedProjects, finishlineProjects, finishedProjects};

for (QListWidget *listWidget : listWidgets)
{
Expand All @@ -97,7 +110,8 @@ void MainWindow::removeProject()
QStringList data = item->text().split("\n");
QString status = listWidget->objectName();

listWidget->takeItem(listWidget->row(item));
int row = listWidget->row(item);
listWidget->takeItem(row);

removeProjectFromDB(&data[1], &status, &data[2]);

Expand All @@ -112,22 +126,23 @@ void MainWindow::getTotalProjects(QTabWidget *projectsTab, QListWidget *notStart
QListWidget *startedProjects, QListWidget *finishedProjects,
QListWidget *finishlineProjects)
{
QThread *totalProjectsThread = new QThread;
QObject::connect(totalProjectsThread, &QThread::started, this,
[this, projectsTab, notStartedProjects, startedProjects, finishedProjects, finishlineProjects]() {
if (projectsTab->currentIndex() == 3)
{
QTimer *timer3 = new QTimer(this);
connect(timer3, &QTimer::timeout, [=]() {
int totalProjects = notStartedProjects->count() + finishlineProjects->count() +
startedProjects->count() + finishedProjects->count();

totalProjectsL->setText(tr("Total projects: ") + QString::number(totalProjects) + " ");
});
timer3->start(500);
}
});
totalProjectsThread->start();
static QTimer *timer3 = new QTimer(this);

connect(timer3, &QTimer::timeout, this, [=]() {
int totalProjects = notStartedProjects->count() + finishlineProjects->count() + startedProjects->count() +
finishedProjects->count();

totalProjectsL->setText(tr("Total projects: ") + QString::number(totalProjects) + " ");
});

if (projectsTab->currentIndex() == 3)
{
timer3->start(500);
}
else
{
timer3->stop();
}
}

void MainWindow::selectFileInQTreeView(QTreeView *treeView, const QString &fileName)
Expand All @@ -136,21 +151,19 @@ void MainWindow::selectFileInQTreeView(QTreeView *treeView, const QString &fileN

void MainWindow::openGitProject()
{
QListWidget *listWidgets[] = {notStartedProjects, startedProjects, finishlineProjects, finishedProjects};
QList<QListWidget *> listWidgets = {notStartedProjects, startedProjects, finishlineProjects, finishedProjects};

for (QListWidget *listWidget : listWidgets)
{
QListWidgetItem *item = listWidget->currentItem();
if (item)
{
QStringList data = item->text().split("\n");

QString git_url = data[1];
QString git_url = item->text().split("\n").value(1);
qDebug() << git_url;

QUrl url(git_url);
QDesktopServices::openUrl(url);

break;
return;
}
}
}
Expand Down
38 changes: 23 additions & 15 deletions src/CodeKeeper/keeperFunc/tasksFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,31 @@ void MainWindow::activateTasksContextMenu(const QPoint &pos, QListWidget *listWi

void MainWindow::onMovingTaskFrom(QListWidgetItem *item, QListWidget *list)
{
qDebug() << "Moving task: " << item->text() << " from: " << list->objectName();

QString task = item->text();
QString status = list->objectName();
if (item && list)
{
qDebug() << "Moving task: " << item->text() << " from: " << list->objectName();
}
}

void MainWindow::onMovingTaskTo(QListWidgetItem *item, QListWidget *list)
{
qDebug() << "Moved task: " << item->text() << " to: " << list->objectName();
if (item && list)
{
qDebug() << "Moved task: " << item->text() << " to: " << list->objectName();

QString task = item->text();
QString status = list->objectName();
QStringList data = task.split("\n");
QString cT = data[1];
QStringList data = item->text().split("\n");
if (data.size() >= 2) // Check if data has at least 2 elements
{
QString status = list->objectName();
QString cT = data[1];

updateTaskStatus(&task, &status, &cT);
updateTaskStatus(&data[0], &status, &cT);
}
else
{
qWarning() << "Invalid task data format";
}
}
}

void MainWindow::addNewTask()
Expand All @@ -75,21 +84,20 @@ void MainWindow::addNewTask()

void MainWindow::removeTask()
{
QListWidget *listWidgets[] = {incompleteTasks, inprocessTasks, completeTasks};
QList<QListWidget *> listWidgets = {incompleteTasks, inprocessTasks, completeTasks};

for (QListWidget *listWidget : listWidgets)
{
QListWidgetItem *item = listWidget->currentItem();
if (item)
{
listWidget->takeItem(listWidget->row(item));
qDebug() << "Removed task: " << item->text();

QString task = item->text();
QString status = listWidget->objectName();

removeTaskFromDB(&task, &status);
listWidget->takeItem(listWidget->row(item));
qDebug() << "Removed task: " << task;

removeTaskFromDB(&task, &status);
delete item;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CodeKeeper/sql_db/tasksDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void MainWindow::loadTasks()
}
else
{
qDebug() << "🟠 Unknown status: " << status;
qDebug() << "Unknown status: " << status;
}
}

Expand Down

0 comments on commit 9d1ca1e

Please sign in to comment.