From 1c20ef1539d6bd88b0dbcdc2f8530b831856fb4b Mon Sep 17 00:00:00 2001
From: Isa Nanic <isa.nanic@savoirfairelinux.com>
Date: Thu, 25 Oct 2018 11:49:30 -0400
Subject: [PATCH] account: update combobox when adding accounts

Change-Id: I2952252bdb3f732ccc0074d053780e1a5648087f
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 callwidget.cpp             |  1 -
 currentaccountcombobox.cpp | 32 ++++++++++++++++++++++----------
 currentaccountcombobox.h   |  7 +++++--
 3 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/callwidget.cpp b/callwidget.cpp
index 8fe1e3f..7c0a4c9 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -97,7 +97,6 @@ CallWidget::CallWidget(QWidget* parent) :
         auto index = Utils::indexInVector(accountList, accountIdToStartWith);
         if (index != -1) {
             ui->currentAccountComboBox->setCurrentIndex(index);
-            ui->currentAccountComboBox->importLabelPhoto(index);
         }
     }
 
diff --git a/currentaccountcombobox.cpp b/currentaccountcombobox.cpp
index 0aa7f5a..d80e54c 100644
--- a/currentaccountcombobox.cpp
+++ b/currentaccountcombobox.cpp
@@ -36,15 +36,14 @@ CurrentAccountComboBox::CurrentAccountComboBox(QWidget* parent)
 {
     Q_UNUSED(parent);
 
-    accountListModel_ = std::make_unique<AccountListModel>();
-    this->setModel(accountListModel_.get());
+    accountListUpdate();
     accountItemDelegate_ = new AccountItemDelegate();
     this->setItemDelegate(accountItemDelegate_);
 
     // combobox index changed and so must the avatar
     connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged),
             [=](const int& index) {
-                importLabelPhoto(index);
+                setCurrentIndex(index);
             });
 
     // account added to combobox
@@ -71,7 +70,7 @@ CurrentAccountComboBox::paintEvent(QPaintEvent* e)
 
     QPoint p(2, 2);
     QPainter painter(this);
-    painter.setRenderHints(QPainter::Antialiasing, QPainter::TextAntialiasing);
+    painter.setRenderHints((QPainter::Antialiasing | QPainter::TextAntialiasing), true);
 
     QStyleOption opt;
     opt.init(this);
@@ -79,8 +78,7 @@ CurrentAccountComboBox::paintEvent(QPaintEvent* e)
 
     // create box in which to draw avatar and presence indicator
     QRect avatarRect(2, 2, cellHeight_, cellHeight_); // [screen awareness]
-    QRect comboBoxRect(52, 2, 322, cellHeight_-4); // [screen awareness]
-
+    QRect comboBoxRect(cellHeight_ + p.x() + 2, 4, this->width() - cellHeight_ + p.x(), cellHeight_- 10); // [screen awareness]
 
     // define and set the two fonts
     QFont fontPrimary = painter.font();
@@ -111,7 +109,7 @@ CurrentAccountComboBox::paintEvent(QPaintEvent* e)
     }
 
     // write primary and secondary account identifiers to combobox label
-    const int elidConst = 85; // [screen awareness]
+    const int elidConst = 80; // [screen awareness]
 
     QString primaryAccountID = QString::fromStdString(Utils::bestNameForAccount(LRCInstance::getCurrentAccountInfo()));
     painter.setPen(Qt::black);
@@ -138,8 +136,22 @@ CurrentAccountComboBox::paintEvent(QPaintEvent* e)
 
 // import account background account pixmap and scale pixmap to fit in label
 void
-CurrentAccountComboBox::importLabelPhoto(const int& index)
+CurrentAccountComboBox::importLabelPhoto(int index)
 {
-    currentAccountAvatarImage_ = accountListModel_->data(accountListModel_->index(index), // [efficiency improvement]
+    currentAccountAvatarImage_ = accountListModel_->data(accountListModel_->index(index, 0), // [efficiency improvement]
         AccountListModel::Role::Picture).value<QPixmap>().scaledToHeight(cellHeight_ - 4, Qt::SmoothTransformation);
-}
\ No newline at end of file
+}
+
+void
+CurrentAccountComboBox::setCurrentIndex(int index)
+{
+    importLabelPhoto(index);
+    QComboBox::setCurrentIndex(index);
+}
+
+void
+CurrentAccountComboBox::accountListUpdate()
+{
+    accountListModel_.reset(new AccountListModel());
+    this->setModel(accountListModel_.get());
+}
diff --git a/currentaccountcombobox.h b/currentaccountcombobox.h
index 5acfe5a..375146e 100644
--- a/currentaccountcombobox.h
+++ b/currentaccountcombobox.h
@@ -28,15 +28,18 @@ namespace Ui {
 class CurrentAccountComboBox : public QComboBox
 {
     Q_OBJECT
-        CurrentAccountComboBox(const CurrentAccountComboBox& cpy);
+    CurrentAccountComboBox(const CurrentAccountComboBox& cpy);
 
 public:
     explicit CurrentAccountComboBox(QWidget* parent = nullptr);
     ~CurrentAccountComboBox();
-    void importLabelPhoto(const int& index);
+    void accountListUpdate();
+    void setCurrentIndex(int index);
+
 
 private:
     void paintEvent(QPaintEvent* e);
+    void importLabelPhoto(int index);
 
     AccountItemDelegate* accountItemDelegate_;
     std::unique_ptr<AccountListModel> accountListModel_;
-- 
GitLab