Skip to content

Commit

Permalink
re-implemented messagebox system
Browse files Browse the repository at this point in the history
- added show image in folder option after exporting diary pixels
- add option to create new diary if no diary was found
- incremented patch version
  • Loading branch information
someretical committed Jan 18, 2022
1 parent c6ba3ed commit 7fe0cdf
Show file tree
Hide file tree
Showing 16 changed files with 413 additions and 367 deletions.
1 change: 1 addition & 0 deletions src/core/googlewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ std::pair<QString, QString> GoogleWrapper::get_file_ids(QByteArray const &data)

for (auto const &file : json["files"]) {
if ("drive#file" == file["kind"]) {
// Don't bother comparing mimeType.
if ("diary.dat" == file["name"]) {
primary_backup_id = QString::fromStdString(file["id"]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/internalmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ void InternalManager::start_update_theme()
{
QPixmapCache::clear();

// There is no memory leak here as Qt deletes the old style for us.
// I made sure to actually check the source code of QApplication.
if (get_theme() == td::Theme::Dark) {
set_dark_palette();
auto s = new DarkStyle;
Expand Down
17 changes: 17 additions & 0 deletions src/core/internalmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,21 @@ class InternalManager : public QObject {
bool diary_file_changed;
};

class AppBusyLock {
AppBusyLock()
{
InternalManager::instance()->start_busy_mode(__LINE__, __func__, __FILE__);
}

~AppBusyLock()
{
InternalManager::instance()->end_busy_mode(__LINE__, __func__, __FILE__);
}

void release()
{
InternalManager::instance()->end_busy_mode(__LINE__, __func__, __FILE__);
}
};

#endif // INTERNAL_MANAGER_H
38 changes: 22 additions & 16 deletions src/gui/diaryeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ bool DiaryEditor::compare_snapshots()
void DiaryEditor::change_month(QDate const &date)
{
auto cb = [this, date](int const res) {
if (QMessageBox::RejectRole == res)
if (QMessageBox::Cancel == res)
return;

if (QMessageBox::AcceptRole)
if (QMessageBox::Save == res)
update_day(true);

// Remove everything from current grid.
Expand Down Expand Up @@ -221,9 +221,9 @@ void DiaryEditor::change_month(QDate const &date)
};

if (!compare_snapshots())
return cmb::confirm_switch_date(this, cb);
return cmb::confirm_switch_entries(this, cb);

cb(QMessageBox::DestructiveRole);
cb(QMessageBox::Discard);
}

void DiaryEditor::render_day(td::CalendarButtonData const &d, bool const set_info_pane)
Expand Down Expand Up @@ -289,10 +289,10 @@ void DiaryEditor::date_clicked(int const day)
return;

auto cb = [this, day](int const res) {
if (QMessageBox::RejectRole == res)
if (QMessageBox::Cancel == res)
return;

if (QMessageBox::AcceptRole == res)
if (QMessageBox::Yes == res)
update_day(true); // Suppress entry saved message so it doesn't appear on new date.

td::CalendarButtonData const old{
Expand All @@ -308,9 +308,9 @@ void DiaryEditor::date_clicked(int const day)
};

if (!compare_snapshots())
cmb::confirm_switch_date(this, cb);
cmb::confirm_switch_entries(this, cb);
else
cb(QMessageBox::DestructiveRole);
cb(QMessageBox::No);
}

void DiaryEditor::update_info_pane(QDate const &date, td::Entry const &entry)
Expand Down Expand Up @@ -365,7 +365,7 @@ void DiaryEditor::update_day(bool const suppress_error)

// Actually try and save the diary.
if (!DiaryHolder::instance()->save() && suppress_error)
return cmb::save_error(this, []() {});
return cmb::display_local_diary_save_error(this);

td::CalendarButtonData const d{std::optional(last_selected_day), std::optional(ui->special_box->isChecked()),
std::optional(static_cast<td::Rating>(ui->rating_dropdown->currentIndex())), std::nullopt, std::nullopt};
Expand All @@ -390,7 +390,7 @@ void DiaryEditor::update_day(bool const suppress_error)
void DiaryEditor::delete_day()
{
auto cb = [this](int const res) {
if (QMessageBox::RejectRole == res)
if (QMessageBox::No == res)
return;

InternalManager::instance()->start_busy_mode(__LINE__, __func__, __FILE__);
Expand All @@ -402,7 +402,7 @@ void DiaryEditor::delete_day()

// Actually try and save the diary.
if (!DiaryHolder::instance()->save())
return cmb::save_error(this, []() {});
return cmb::display_local_diary_save_error(this);

ui->alert_text->setText("Deleted entry.");
ui->alert_text->update();
Expand All @@ -420,11 +420,17 @@ void DiaryEditor::delete_day()
};

// Shift click bypasses confirmation dialog.
if (Qt::ShiftModifier != QGuiApplication::keyboardModifiers())
return cmb::yn_messagebox(
this, cb, "Are you sure you want to delete this entry?", "This action is not reversible!");

cb(QMessageBox::AcceptRole);
if (Qt::ShiftModifier == QGuiApplication::keyboardModifiers())
return cb(QMessageBox::Yes);

auto msgbox = new QMessageBox(this);
msgbox->setAttribute(Qt::WA_DeleteOnClose, true);
msgbox->setText("Are you sure you want to delete this entry?");
msgbox->setInformativeText("This action is not reversible!");
msgbox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgbox->setDefaultButton(QMessageBox::No);
QObject::connect(msgbox, &QMessageBox::finished, cb);
msgbox->show();
}

void DiaryEditor::reset_day()
Expand Down
45 changes: 31 additions & 14 deletions src/gui/diarypixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,17 @@ void DiaryPixels::update_theme() {}
int DiaryPixels::calculate_size()
{
// Minimum dimensions:
// Width of button 36px
// Width of frame 1218px
// Height of frame 570px
// 32 labels * 36px per row
// 31 gaps of 2px

updateGeometry();

// For every 500px width/height, increase gap by 1px.
int gap = 0;
int w = ui->hidden_frame->width();
int h = ui->hidden_frame->height();
int w = ui->hidden_frame->width() - 18; // - 18 because the hidden_frame has margins of 9px on all sides.
int h = ui->hidden_frame->height() - 18;
int top = std::max({w, h, 1218});

// For every 500px width/height, increase gap by 1px.
while ((top -= 500) > -1)
gap++;

Expand Down Expand Up @@ -201,7 +199,7 @@ void DiaryPixels::export_image()

auto bold_font = QApplication::font();
bold_font.setBold(true);
bold_font.setPointSize(20);
bold_font.setPointSize(24);
p.setFont(bold_font);

QRectF rect;
Expand All @@ -215,13 +213,32 @@ void DiaryPixels::export_image()
pixmap = ui->hidden_frame->grab();
}

if (pixmap.save(filename))
cmb::ok_messagebox(
this, []() {}, "Image exported.", "");
else
cmb::ok_messagebox(
this, []() {}, "The app could not write to the specified location.",
"This usually means a permission error.");
if (pixmap.save(filename)) {
auto cb = [this, filename](int const res) {
if (QMessageBox::Yes != res)
return;

QFileInfo info(filename);
if (!QDesktopServices::openUrl(info.dir().path())) {
auto msgbox = new QMessageBox(this);
msgbox->setAttribute(Qt::WA_DeleteOnClose, true);
msgbox->setText("Failed to show file in explorer.");
msgbox->setStandardButtons(QMessageBox::Ok);
return msgbox->show();
}
};

auto msgbox = new QMessageBox(this);
msgbox->setAttribute(Qt::WA_DeleteOnClose, true);
msgbox->setText("Would you like to open the folder containing the exported image?");
msgbox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgbox->setDefaultButton(QMessageBox::No);
connect(msgbox, &QMessageBox::finished, cb);
msgbox->show();
}
else {
return cmb::display_local_io_error(this);
}
}

void DiaryPixels::resizeEvent(QResizeEvent *)
Expand Down
143 changes: 33 additions & 110 deletions src/gui/diarypixels.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QFrame" name="hidden_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="1">
<layout class="QGridLayout" name="grid"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_3">
<property name="verticalSpacing">
Expand Down Expand Up @@ -107,116 +140,6 @@
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QFrame" name="hidden_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<property name="leftMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="1" column="1">
<layout class="QGridLayout" name="grid">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</item>
<item row="2" column="2">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<tabstops>
Expand Down
Loading

0 comments on commit 7fe0cdf

Please sign in to comment.