Chat-O-Matic/application/windows/UserInfoWindow.cpp

173 lines
3.7 KiB
C++
Raw Permalink Normal View History

/*
* Copyright 2012, Casalinuovo Dario. All rights reserved.
2021-07-07 22:35:44 -05:00
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Casalinuovo Dario
2021-07-07 22:35:44 -05:00
* Jaidyn Levesque <jadedctrl@teknik.io>
*/
#include "UserInfoWindow.h"
2021-05-19 16:12:19 -05:00
#include <Alert.h>
2021-07-24 20:54:46 -05:00
#include <Catalog.h>
2021-07-07 22:35:44 -05:00
#include <LayoutBuilder.h>
#include <Message.h>
2021-05-19 16:12:19 -05:00
#include <libinterface/BitmapView.h>
2021-07-24 20:54:46 -05:00
#include "ImageCache.h"
#include "NotifyMessage.h"
#include "User.h"
2021-07-24 20:54:46 -05:00
#include "Utils.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "UserInfoWindow"
UserInfoWindow::UserInfoWindow(User* user)
:
2021-07-24 20:54:46 -05:00
BWindow(BRect(200, 200, 300, 400),
2021-08-15 15:45:28 -05:00
B_TRANSLATE("User information"), B_FLOATING_WINDOW,
B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fUser(user)
{
2021-07-07 22:35:44 -05:00
_InitInterface();
MoveTo(BAlert::AlertPosition(Bounds().Width(), Bounds().Height() / 2));
fUser->RegisterObserver(this);
}
UserInfoWindow::~UserInfoWindow()
{
fUser->UnregisterObserver(this);
2021-07-07 22:35:44 -05:00
}
2021-07-07 22:35:44 -05:00
void
UserInfoWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
default:
BWindow::MessageReceived(message);
break;
}
}
void
UserInfoWindow::ObserveString(int32 what, BString string)
{
Lock();
switch (what) {
case STR_CONTACT_NAME:
fNameLabel->SetText(string);
break;
case STR_PERSONAL_STATUS:
fTextStatusLabel->SetText(string);
break;
}
Unlock();
}
void
UserInfoWindow::ObserveInteger(int32 what, int32 num)
{
Lock();
switch (what) {
case INT_CONTACT_STATUS:
_UpdateStatusViews((UserStatus)num);
break;
}
Unlock();
}
void
UserInfoWindow::ObservePointer(int32 what, void* ptr)
{
Lock();
switch (what) {
case PTR_AVATAR_BITMAP:
fAvatar->SetBitmap((BBitmap*)ptr);
break;
}
Unlock();
}
2021-07-07 22:35:44 -05:00
void
UserInfoWindow::_InitInterface()
{
fNameLabel = new BStringView("nameLabel", fUser->GetName().String());
2021-07-24 20:54:46 -05:00
fNameLabel->SetFont(be_bold_font);
fStatusLabel = new BStringView("statusLabel", "");
2021-07-24 20:54:46 -05:00
float iconSize = be_plain_font->Size() + 5;
fStatusIcon = new BitmapView("statusIcon");
fStatusIcon->SetExplicitMaxSize(BSize(iconSize, iconSize));
2021-07-07 22:35:44 -05:00
fTextStatusLabel = new BStringView("statusMessageLabel",
fUser->GetNotifyPersonalStatus());
_UpdateStatusViews(fUser->GetNotifyStatus());
2021-07-24 20:54:46 -05:00
const char* userId = fUser->GetId().String();
fIdLabel = new BTextView("idLabel");
fIdLabel->SetText(userId);
fIdLabel->SetFont(be_fixed_font);
fIdLabel->SetWordWrap(false);
fIdLabel->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
fIdLabel->MakeEditable(false);
fIdLabel->SetExplicitMinSize(
BSize(be_fixed_font->StringWidth(userId), B_SIZE_UNSET));
2021-07-24 20:54:46 -05:00
fAvatar = new BitmapView("userIcon");
fAvatar->SetExplicitMaxSize(BSize(70, 70));
fAvatar->SetExplicitMinSize(BSize(50, 50));
fAvatar->SetExplicitPreferredSize(BSize(50, 50));
fAvatar->SetBitmap(fUser->AvatarBitmap());
2021-07-07 22:35:44 -05:00
// Centering is lyfeee
fNameLabel->SetExplicitAlignment(BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
fIdLabel->SetExplicitAlignment(BAlignment(B_ALIGN_CENTER, B_ALIGN_TOP));
2021-07-24 20:54:46 -05:00
fStatusIcon->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_TOP));
fStatusLabel->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_TOP));
fAvatar->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_TOP));
2021-07-07 22:35:44 -05:00
BLayoutBuilder::Group<>(this, B_HORIZONTAL, 10)
.SetInsets(B_USE_DEFAULT_SPACING)
.AddGroup(B_VERTICAL)
.Add(fNameLabel)
.Add(fIdLabel)
.Add(fTextStatusLabel)
.AddGlue()
.End()
2021-07-07 22:35:44 -05:00
.AddGroup(B_VERTICAL)
.AddGroup(B_VERTICAL)
.Add(fAvatar)
2021-07-24 20:54:46 -05:00
.AddGroup(B_HORIZONTAL)
.Add(fStatusIcon)
.Add(fStatusLabel)
.End()
.End()
2021-07-07 22:35:44 -05:00
.End();
}
void
UserInfoWindow::_UpdateStatusViews(UserStatus status)
{
fStatusLabel->SetText(UserStatusToString(status));
BBitmap* statusBitmap =
ImageCache::Get()->GetImage(UserStatusToImageKey(status));
fStatusIcon->SetBitmap(statusBitmap);
}