Skip to content

Commit

Permalink
Slight redesign of category windows
Browse files Browse the repository at this point in the history
  • Loading branch information
JadedCtrl authored and humdinger committed Mar 20, 2021
1 parent 851e347 commit 6a6bb0b
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 113 deletions.
130 changes: 50 additions & 80 deletions src/CategoryEditWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

CategoryEditWindow::CategoryEditWindow()
:
BWindow(BRect(), B_TRANSLATE("Category Edit"), B_TITLED_WINDOW,
BWindow(BRect(), B_TRANSLATE("Edit category"), B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS)
{
_InitInterface();
Expand All @@ -51,16 +51,16 @@ CategoryEditWindow::MessageReceived(BMessage* message)
break;
}

case kSavePressed:
_OnSavePressed();
case kCategoryTextChanged:
_CategoryModified();
break;

case kDeletePressed:
_OnDeletePressed();
case kOkPressed:
_OnOkPressed();
break;

case kCategoryTextChanged:
_CategoryModified();
case kRevertPressed:
_OnRevertPressed();
break;

default:
Expand All @@ -73,6 +73,7 @@ CategoryEditWindow::MessageReceived(BMessage* message)
bool
CategoryEditWindow::QuitRequested()
{
_RefreshWindows();
((App*)be_app)->categoryWindow()->PostMessage(kCategoryEditQuitting);
return true;
}
Expand All @@ -84,23 +85,23 @@ CategoryEditWindow::SetCategory(Category* category)
fCategory = category;

if (fCategory != NULL) {
fCategoryText->SetText(category->GetName());
fPicker->SetValue(category->GetColor());
fColorPreview->SetColor(category->GetColor());
fColorPreview->Invalidate();
fOriginalText = category->GetName();
fOriginalColor = category->GetColor();

fSaveButton->SetEnabled(false);
fCategoryText->SetText(fOriginalText);
fPicker->SetValue(fOriginalColor);
fColorPreview->SetColor(fOriginalColor);
fColorPreview->Invalidate();
}

else
{
fPicker->SetValue((rgb_color){255, 255, 0});
fColorPreview->SetColor((rgb_color){255, 255, 0});
fColorPreview->Invalidate();
fDeleteButton->SetEnabled(false);

}

fRevertButton->SetEnabled(false);
fCategoryText->SetModificationMessage(new BMessage(kCategoryTextChanged));
}

Expand All @@ -112,10 +113,10 @@ CategoryEditWindow::_InitInterface()
fCategoryText = new BTextControl("CategoryText", NULL,
B_TRANSLATE("New category"), new BMessage(kCategoryTextChanged));

fSaveButton = new BButton("SaveButton", B_TRANSLATE("Save"),
new BMessage(kSavePressed));
fDeleteButton = new BButton("DeleteButton", B_TRANSLATE("Delete"),
new BMessage(kDeletePressed));
fOkButton = new BButton("OkButton", B_TRANSLATE("OK"),
new BMessage(kOkPressed));
fRevertButton = new BButton("RevertButton", B_TRANSLATE("Revert"),
new BMessage(kRevertPressed));


BRect wellrect(0, 0, 49, 49);
Expand Down Expand Up @@ -143,9 +144,9 @@ CategoryEditWindow::_InitInterface()
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING,
B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
.Add(fRevertButton)
.AddGlue()
.Add(fSaveButton)
.Add(fDeleteButton)
.Add(fOkButton)
.End()
.End();

Expand All @@ -166,56 +167,26 @@ CategoryEditWindow::_SetCurrentColor(rgb_color color)
void
CategoryEditWindow::_CategoryModified()
{
fSaveButton->SetEnabled(true);
if (fOriginalText == NULL)
return;
fRevertButton->SetEnabled(true);
}


void
CategoryEditWindow::_OnDeletePressed()
CategoryEditWindow::_OnRevertPressed()
{
BString defaultCat = ((App*)be_app)->GetPreferences()->fDefaultCategory;

if (BString(fCategoryText->Text()) == defaultCat) {
BAlert* alert = new BAlert(B_TRANSLATE("Error"),
B_TRANSLATE("You cannot delete the default category."),
NULL, B_TRANSLATE("OK"),NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);

alert->Go();
return;
}

BAlert* alert = new BAlert(B_TRANSLATE("Confirm delete"),
B_TRANSLATE("Are you sure you want to delete the selected category?"),
NULL, B_TRANSLATE("OK"), B_TRANSLATE("Cancel"),
B_WIDTH_AS_USUAL, B_WARNING_ALERT);

alert->SetShortcut(1, B_ESCAPE);
int32 button_index = alert->Go();

if (button_index == 0) {

CategoryWindow* parent = ((App*)be_app)->categoryWindow();
if (parent->GetDBManager()->RemoveCategory(fCategory)) {
parent->LoadCategories();
_RefreshWindows();
_CloseWindow();
}
else
{
BAlert* alert = new BAlert(B_TRANSLATE("Error"),
B_TRANSLATE("Cannot delete category. Can't delete a category used by events."),
NULL, B_TRANSLATE("OK"),NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
alert->Go();
return;
}

}
fCategoryText->SetText(fOriginalText);
fPicker->SetValue(fOriginalColor);
fColorPreview->SetColor(fOriginalColor);
fColorPreview->Invalidate();
}


void
CategoryEditWindow::_CloseWindow()
{
_RefreshWindows();
PostMessage(B_QUIT_REQUESTED);
}

Expand All @@ -228,10 +199,9 @@ CategoryEditWindow::_RefreshWindows()


void
CategoryEditWindow::_OnSavePressed()
CategoryEditWindow::_OnOkPressed()
{
if (BString(fCategoryText->Text()).CountChars() < 3) {

BAlert* alert = new BAlert(B_TRANSLATE("Error"),
B_TRANSLATE("The name must have a length greater than 2."),
NULL, B_TRANSLATE("OK"),NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
Expand All @@ -240,30 +210,30 @@ CategoryEditWindow::_OnSavePressed()
return;
}

Category category(fCategoryText->Text(), fPicker->ValueAsColor());
CategoryWindow* parent = ((App*)be_app)->categoryWindow();

if ((fCategory == NULL) && (parent->GetDBManager()->AddCategory(&category))) {
parent->LoadCategories();
_RefreshWindows();
_CloseWindow();
}

else if ((fCategory != NULL) && (parent->GetDBManager()->UpdateCategory(fCategory, &category)))
{
parent->LoadCategories();
_RefreshWindows();
_CloseWindow();
}

else
{
if (_SaveChanges() == false) {
BAlert* alert = new BAlert(B_TRANSLATE("Error"),
B_TRANSLATE("Cannot add/modify the category. A category with the same name or color already exists."),
NULL, B_TRANSLATE("OK"),NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
alert->Go();
return;

}
_CloseWindow();
}


bool
CategoryEditWindow::_SaveChanges()
{
if (BString(fCategoryText->Text()).CountChars() > 3) {
Category category(fCategoryText->Text(), fPicker->ValueAsColor());
CategoryWindow* parent = ((App*)be_app)->categoryWindow();

if (fCategory == NULL)
return parent->GetDBManager()->AddCategory(&category);
else if (fCategory != NULL)
return parent->GetDBManager()->UpdateCategory(fCategory, &category);
}
return false;
}


22 changes: 13 additions & 9 deletions src/CategoryEditWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@ class CategoryEditWindow: public BWindow {
private:
void _InitInterface();
void _SetCurrentColor(rgb_color color);
void _OnSavePressed();
void _OnDeletePressed();
void _OnOkPressed();
void _OnRevertPressed();
void _CategoryModified();
void _RefreshWindows();
void _CloseWindow();
bool _SaveChanges();

static const uint32 kSavePressed = 1000;
static const uint32 kDeletePressed = 1001;
static const uint32 kOkPressed = 1000;
static const uint32 kRevertPressed = 1001;
static const uint32 kCategoryTextChanged = 1002;

BView* fMainView;
BTextControl* fCategoryText;
BColorControl* fPicker;
ColorPreview* fColorPreview;
BButton* fSaveButton;
BButton* fDeleteButton;
BTextControl* fCategoryText;
BColorControl* fPicker;
ColorPreview* fColorPreview;
BButton* fOkButton;
BButton* fRevertButton;

BString fOriginalText;
rgb_color fOriginalColor;

Category* fCategory;
};
Expand Down
Loading

0 comments on commit 6a6bb0b

Please sign in to comment.