1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: use enum class

This commit is contained in:
Nils Fenner 2016-08-19 14:22:51 +02:00 committed by Diadlo
parent de78e0dddb
commit 4b7fc5792d
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 129 additions and 119 deletions

View File

@ -731,16 +731,16 @@ void Widget::onSeparateWindowChanged(bool separate, bool clicked)
if (settingsWidget) if (settingsWidget)
{ {
ContentLayout* contentLayout = createContentDialog((SettingDialog)); ContentLayout* contentLayout = createContentDialog((DialogType::SettingDialog));
contentLayout->parentWidget()->resize(size); contentLayout->parentWidget()->resize(size);
contentLayout->parentWidget()->move(pos); contentLayout->parentWidget()->move(pos);
settingsWidget->show(contentLayout); settingsWidget->show(contentLayout);
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
} }
setWindowTitle(QString()); setWindowTitle(QString());
setActiveToolMenuButton(None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
} }
@ -771,17 +771,17 @@ void Widget::onAddClicked()
{ {
if (!addFriendForm->isShown()) if (!addFriendForm->isShown())
{ {
addFriendForm->show(createContentDialog(AddDialog)); addFriendForm->show(createContentDialog(DialogType::AddDialog));
} }
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
else else
{ {
hideMainForms(nullptr); hideMainForms(nullptr);
addFriendForm->show(contentLayout); addFriendForm->show(contentLayout);
setWindowTitle(fromDialogType(AddDialog)); setWindowTitle(fromDialogType(DialogType::AddDialog));
setActiveToolMenuButton(Widget::AddButton); setActiveToolMenuButton(ActiveToolMenuButton::AddButton);
} }
} }
@ -791,17 +791,17 @@ void Widget::onGroupClicked()
{ {
if (!groupInviteForm->isShown()) if (!groupInviteForm->isShown())
{ {
groupInviteForm->show(createContentDialog(GroupDialog)); groupInviteForm->show(createContentDialog(DialogType::GroupDialog));
} }
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
else else
{ {
hideMainForms(nullptr); hideMainForms(nullptr);
groupInviteForm->show(contentLayout); groupInviteForm->show(contentLayout);
setWindowTitle(fromDialogType(GroupDialog)); setWindowTitle(fromDialogType(DialogType::GroupDialog));
setActiveToolMenuButton(Widget::GroupButton); setActiveToolMenuButton(ActiveToolMenuButton::GroupButton);
} }
} }
@ -811,17 +811,17 @@ void Widget::onTransferClicked()
{ {
if (!filesForm->isShown()) if (!filesForm->isShown())
{ {
filesForm->show(createContentDialog(TransferDialog)); filesForm->show(createContentDialog(DialogType::TransferDialog));
} }
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
else else
{ {
hideMainForms(nullptr); hideMainForms(nullptr);
filesForm->show(contentLayout); filesForm->show(contentLayout);
setWindowTitle(fromDialogType(TransferDialog)); setWindowTitle(fromDialogType(DialogType::TransferDialog));
setActiveToolMenuButton(Widget::TransferButton); setActiveToolMenuButton(ActiveToolMenuButton::TransferButton);
} }
} }
@ -898,18 +898,18 @@ void Widget::onShowSettings()
{ {
if (!settingsWidget->isShown()) if (!settingsWidget->isShown())
{ {
settingsWidget->show(createContentDialog(SettingDialog)); settingsWidget->show(createContentDialog(DialogType::SettingDialog));
} }
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg")); settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg"));
} }
else else
{ {
hideMainForms(nullptr); hideMainForms(nullptr);
settingsWidget->show(contentLayout); settingsWidget->show(contentLayout);
setWindowTitle(fromDialogType(SettingDialog)); setWindowTitle(fromDialogType(DialogType::SettingDialog));
setActiveToolMenuButton(Widget::SettingButton); setActiveToolMenuButton(ActiveToolMenuButton::SettingButton);
} }
} }
@ -919,24 +919,24 @@ void Widget::showProfile() // onAvatarClicked, onUsernameClicked
{ {
if (!profileForm->isShown()) if (!profileForm->isShown())
{ {
profileForm->show(createContentDialog(ProfileDialog)); profileForm->show(createContentDialog(DialogType::ProfileDialog));
} }
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg")); settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg"));
} }
else else
{ {
hideMainForms(nullptr); hideMainForms(nullptr);
profileForm->show(contentLayout); profileForm->show(contentLayout);
setWindowTitle(fromDialogType(ProfileDialog)); setWindowTitle(fromDialogType(DialogType::ProfileDialog));
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
} }
} }
void Widget::hideMainForms(GenericChatroomWidget* chatroomWidget) void Widget::hideMainForms(GenericChatroomWidget* chatroomWidget)
{ {
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(ActiveToolMenuButton::None);
if (contentLayout != nullptr) if (contentLayout != nullptr)
{ {
@ -1055,7 +1055,7 @@ void Widget::addFriend(int friendId, const ToxPk& friendPk)
widget->onAvatarChange(friendId, avatar); widget->onAvatarChange(friendId, avatar);
} }
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
widget->search(ui->searchContactText->text(), filterOffline(filter)); widget->search(ui->searchContactText->text(), filterOffline(filter));
} }
@ -1154,7 +1154,7 @@ void Widget::onFriendAliasChanged(uint32_t friendId, const QString& alias)
Status s = f->getStatus(); Status s = f->getStatus();
contactListWidget->moveWidget(friendWidget, s); contactListWidget->moveWidget(friendWidget, s);
int criteria = getFilterCriteria(); FilterCriteria criteria = getFilterCriteria();
bool filter = s == Status::Offline ? filterOffline(criteria) bool filter = s == Status::Offline ? filterOffline(criteria)
: filterOnline(criteria); : filterOnline(criteria);
friendWidget->searchName(ui->searchContactText->text(), filter); friendWidget->searchName(ui->searchContactText->text(), filter);
@ -1448,19 +1448,20 @@ QString Widget::fromDialogType(DialogType type)
{ {
switch (type) switch (type)
{ {
case AddDialog: case DialogType::AddDialog:
return tr("Add friend", "title of the window"); return tr("Add friend", "title of the window");
case GroupDialog: case DialogType::GroupDialog:
return tr("Group invites", "title of the window"); return tr("Group invites", "title of the window");
case TransferDialog: case DialogType::TransferDialog:
return tr("File transfers", "title of the window"); return tr("File transfers", "title of the window");
case SettingDialog: case DialogType::SettingDialog:
return tr("Settings", "title of the window"); return tr("Settings", "title of the window");
case ProfileDialog: case DialogType::ProfileDialog:
return tr("My profile", "title of the window"); return tr("My profile", "title of the window");
default:
return QString();
} }
assert(false);
return QString();
} }
bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool sound, bool notify) bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool sound, bool notify)
@ -1829,7 +1830,7 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q
contactListWidget->renameGroupWidget(g->getGroupWidget(), title); contactListWidget->renameGroupWidget(g->getGroupWidget(), title);
g->setName(title); g->setName(title);
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
g->getGroupWidget()->searchName(ui->searchContactText->text(), filterGroups(filter)); g->getGroupWidget()->searchName(ui->searchContactText->text(), filterGroups(filter));
} }
@ -1918,7 +1919,7 @@ Group *Widget::createGroup(int groupId)
connect(newgroup->getChatForm(), &GroupChatForm::sendAction, core, &Core::sendGroupAction); connect(newgroup->getChatForm(), &GroupChatForm::sendAction, core, &Core::sendGroupAction);
connect(newgroup->getChatForm(), &GroupChatForm::groupTitleChanged, core, &Core::changeGroupTitle); connect(newgroup->getChatForm(), &GroupChatForm::groupTitleChanged, core, &Core::changeGroupTitle);
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
newgroup->getGroupWidget()->searchName(ui->searchContactText->text(), filterGroups(filter)); newgroup->getGroupWidget()->searchName(ui->searchContactText->text(), filterGroups(filter));
return newgroup; return newgroup;
@ -1952,38 +1953,40 @@ bool Widget::event(QEvent * e)
{ {
switch (e->type()) switch (e->type())
{ {
case QEvent::MouseButtonPress: case QEvent::MouseButtonPress:
case QEvent::MouseButtonDblClick: case QEvent::MouseButtonDblClick:
focusChatInput(); focusChatInput();
break; break;
case QEvent::Paint: case QEvent::Paint:
ui->friendList->updateVisualTracking(); ui->friendList->updateVisualTracking();
break; break;
case QEvent::WindowActivate: case QEvent::WindowActivate:
if (activeChatroomWidget != nullptr) if (activeChatroomWidget)
{ {
activeChatroomWidget->resetEventFlags(); activeChatroomWidget->resetEventFlags();
activeChatroomWidget->updateStatusLight(); activeChatroomWidget->updateStatusLight();
setWindowTitle(activeChatroomWidget->getTitle()); setWindowTitle(activeChatroomWidget->getTitle());
} }
if (eventFlag) if (eventFlag)
resetIcon(); {
resetIcon();
}
focusChatInput(); focusChatInput();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
emit windowStateChanged(windowState()); emit windowStateChanged(windowState());
case QEvent::WindowStateChange: case QEvent::WindowStateChange:
Nexus::getInstance().updateWindowsStates(); Nexus::getInstance().updateWindowsStates();
#endif #endif
break; break;
default: default:
break; break;
} }
return QWidget::event(e); return QMainWindow::event(e);
} }
void Widget::onUserAwayCheck() void Widget::onUserAwayCheck()
@ -2181,7 +2184,7 @@ void Widget::cycleContacts(bool forward)
contactListWidget->cycleContacts(activeChatroomWidget, forward); contactListWidget->cycleContacts(activeChatroomWidget, forward);
} }
bool Widget::filterGroups(int index) bool Widget::filterGroups(FilterCriteria index)
{ {
switch (index) switch (index)
{ {
@ -2193,7 +2196,7 @@ bool Widget::filterGroups(int index)
} }
} }
bool Widget::filterOffline(int index) bool Widget::filterOffline(FilterCriteria index)
{ {
switch (index) switch (index)
{ {
@ -2205,7 +2208,7 @@ bool Widget::filterOffline(int index)
} }
} }
bool Widget::filterOnline(int index) bool Widget::filterOnline(FilterCriteria index)
{ {
switch (index) switch (index)
{ {
@ -2305,9 +2308,11 @@ QString Widget::getStatusTitle(Status status)
case Status::Busy: case Status::Busy:
return QStringLiteral("busy"); return QStringLiteral("busy");
case Status::Offline: case Status::Offline:
default:
return QStringLiteral("offline"); return QStringLiteral("offline");
} }
assert(false);
return QStringLiteral("");
} }
Status Widget::getStatusFromString(QString status) Status Widget::getStatusFromString(QString status)
@ -2325,9 +2330,12 @@ Status Widget::getStatusFromString(QString status)
void Widget::searchContacts() void Widget::searchContacts()
{ {
QString searchString = ui->searchContactText->text(); QString searchString = ui->searchContactText->text();
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
contactListWidget->searchChatrooms(searchString, filterOnline(filter), filterOffline(filter), filterGroups(filter)); contactListWidget->searchChatrooms(searchString,
filterOnline(filter),
filterOffline(filter),
filterGroups(filter));
updateFilterText(); updateFilterText();
@ -2361,25 +2369,25 @@ void Widget::updateFilterText()
ui->searchContactFilterBox->setText(text); ui->searchContactFilterBox->setText(text);
} }
int Widget::getFilterCriteria() const Widget::FilterCriteria Widget::getFilterCriteria() const
{ {
QAction* checked = filterGroup->checkedAction(); QAction* checked = filterGroup->checkedAction();
if (checked == filterOnlineAction) if (checked == filterOnlineAction)
return Online; return FilterCriteria::Online;
else if (checked == filterOfflineAction) else if (checked == filterOfflineAction)
return Offline; return FilterCriteria::Offline;
else if (checked == filterFriendsAction) else if (checked == filterFriendsAction)
return Friends; return FilterCriteria::Friends;
else if (checked == filterGroupsAction) else if (checked == filterGroupsAction)
return Groups; return FilterCriteria::Groups;
return All; return FilterCriteria::All;
} }
void Widget::searchCircle(CircleWidget *circleWidget) void Widget::searchCircle(CircleWidget *circleWidget)
{ {
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
QString text = ui->searchContactText->text(); QString text = ui->searchContactText->text();
circleWidget->search(text, true, filterOnline(filter), filterOffline(filter)); circleWidget->search(text, true, filterOnline(filter), filterOffline(filter));
} }
@ -2387,7 +2395,7 @@ void Widget::searchCircle(CircleWidget *circleWidget)
void Widget::searchItem(GenericChatItemWidget *chatItem, GenericChatItemWidget::ItemType type) void Widget::searchItem(GenericChatItemWidget *chatItem, GenericChatItemWidget::ItemType type)
{ {
bool hide; bool hide;
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
switch (type) switch (type)
{ {
case GenericChatItemWidget::GroupItem: case GenericChatItemWidget::GroupItem:
@ -2402,7 +2410,7 @@ void Widget::searchItem(GenericChatItemWidget *chatItem, GenericChatItemWidget::
bool Widget::groupsVisible() const bool Widget::groupsVisible() const
{ {
int filter = getFilterCriteria(); FilterCriteria filter = getFilterCriteria();
return !filterGroups(filter); return !filterGroups(filter);
} }
@ -2482,14 +2490,14 @@ void Widget::groupInvitesClear()
void Widget::setActiveToolMenuButton(ActiveToolMenuButton newActiveButton) void Widget::setActiveToolMenuButton(ActiveToolMenuButton newActiveButton)
{ {
ui->addButton->setChecked(newActiveButton == Widget::AddButton); ui->addButton->setChecked(newActiveButton == ActiveToolMenuButton::AddButton);
ui->addButton->setDisabled(newActiveButton == Widget::AddButton); ui->addButton->setDisabled(newActiveButton == ActiveToolMenuButton::AddButton);
ui->groupButton->setChecked(newActiveButton == Widget::GroupButton); ui->groupButton->setChecked(newActiveButton == ActiveToolMenuButton::GroupButton);
ui->groupButton->setDisabled(newActiveButton == Widget::GroupButton); ui->groupButton->setDisabled(newActiveButton == ActiveToolMenuButton::GroupButton);
ui->transferButton->setChecked(newActiveButton == Widget::TransferButton); ui->transferButton->setChecked(newActiveButton == ActiveToolMenuButton::TransferButton);
ui->transferButton->setDisabled(newActiveButton == Widget::TransferButton); ui->transferButton->setDisabled(newActiveButton == ActiveToolMenuButton::TransferButton);
ui->settingsButton->setChecked(newActiveButton == Widget::SettingButton); ui->settingsButton->setChecked(newActiveButton == ActiveToolMenuButton::SettingButton);
ui->settingsButton->setDisabled(newActiveButton == Widget::SettingButton); ui->settingsButton->setDisabled(newActiveButton == ActiveToolMenuButton::SettingButton);
} }
void Widget::retranslateUi() void Widget::retranslateUi()
@ -2518,7 +2526,7 @@ void Widget::retranslateUi()
actionShow->setText(tr("Show", "Tray action menu to show qTox window")); actionShow->setText(tr("Show", "Tray action menu to show qTox window"));
if (!Settings::getInstance().getSeparateWindow()) if (!Settings::getInstance().getSeparateWindow())
setWindowTitle(fromDialogType(AddDialog)); setWindowTitle(fromDialogType(DialogType::AddDialog));
friendRequestsUpdate(); friendRequestsUpdate();
groupInvitesUpdate(); groupInvitesUpdate();

View File

@ -65,6 +65,35 @@ class QPushButton;
class Widget final : public QMainWindow class Widget final : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
private:
enum class ActiveToolMenuButton
{
AddButton,
GroupButton,
TransferButton,
SettingButton,
None,
};
enum class DialogType
{
AddDialog,
TransferDialog,
SettingDialog,
ProfileDialog,
GroupDialog
};
enum class FilterCriteria
{
All = 0,
Online,
Offline,
Friends,
Groups
};
public: public:
explicit Widget(QWidget* parent = 0); explicit Widget(QWidget* parent = 0);
~Widget(); ~Widget();
@ -82,15 +111,6 @@ public:
void updateIcons(); void updateIcons();
void clearContactsList(); void clearContactsList();
enum DialogType
{
AddDialog,
TransferDialog,
SettingDialog,
ProfileDialog,
GroupDialog
};
static QString fromDialogType(DialogType type); static QString fromDialogType(DialogType type);
ContentDialog* createContentDialog() const; ContentDialog* createContentDialog() const;
ContentLayout* createContentDialog(DialogType type) const; ContentLayout* createContentDialog(DialogType type) const;
@ -200,24 +220,6 @@ private slots:
private: private:
int icon_size; int icon_size;
private:
enum ActiveToolMenuButton {
AddButton,
GroupButton,
TransferButton,
SettingButton,
None,
};
enum FilterCriteria
{
All=0,
Online,
Offline,
Friends,
Groups
};
private: private:
bool newMessageAlert(QWidget* currentWindow, bool isActive, bool sound = true, bool notify = true); bool newMessageAlert(QWidget* currentWindow, bool isActive, bool sound = true, bool notify = true);
void setActiveToolMenuButton(ActiveToolMenuButton newActiveButton); void setActiveToolMenuButton(ActiveToolMenuButton newActiveButton);
@ -231,10 +233,10 @@ private:
void searchContacts(); void searchContacts();
void changeDisplayMode(); void changeDisplayMode();
void updateFilterText(); void updateFilterText();
int getFilterCriteria() const; FilterCriteria getFilterCriteria() const;
static bool filterGroups(int index); static bool filterGroups(FilterCriteria index);
static bool filterOnline(int index); static bool filterOnline(FilterCriteria index);
static bool filterOffline(int index); static bool filterOffline(FilterCriteria index);
void retranslateUi(); void retranslateUi();
void focusChatInput(); void focusChatInput();