selectaddresstypecombobox.cpp 3.47 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4
/*
    This file is part of Akonadi Contact.

    Copyright (C) 2016 eyeOS S.L.U., a Telefonica company, sales@eyeos.com
Laurent Montel's avatar
Laurent Montel committed
5
    Copyright (C) 2016-2017 Laurent Montel <laurent.montel@kdab.com>
Laurent Montel's avatar
Laurent Montel committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

23
#include "addresstypedialog.h"
Laurent Montel's avatar
Laurent Montel committed
24 25
#include "selectaddresstypecombobox.h"
#include <KLocalizedString>
26
#include <autoqpointer_p.h>
Laurent Montel's avatar
Laurent Montel committed
27
using namespace Akonadi;
Laurent Montel's avatar
Laurent Montel committed
28 29 30 31 32 33
SelectAddressTypeComboBox::SelectAddressTypeComboBox(QWidget *parent)
    : KComboBox(parent)
    , mType(KContacts::Address::Home)
    , mLastSelected(0)
{
    const int count = KContacts::Address::typeList().count();
34 35
    mTypeList.reserve(count + 2);
    mTypeList.append(-2); // Add initial message
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39 40 41 42
    for (int i = 0; i < count; ++i) {
        mTypeList.append(KContacts::Address::typeList().at(i));
    }
    mTypeList.append(-1);   // Others...

    update();

43
    connect(this, SIGNAL(activated(int)), this, SLOT(selected(int)));
Laurent Montel's avatar
Laurent Montel committed
44 45 46 47 48 49 50 51
}

SelectAddressTypeComboBox::~SelectAddressTypeComboBox()
{
}

void SelectAddressTypeComboBox::setType(KContacts::Address::Type type)
{
Laurent Montel's avatar
Laurent Montel committed
52
    if (!mTypeList.contains(static_cast<int>(type))) {
Laurent Montel's avatar
Laurent Montel committed
53
        // insert at the end, but before the 'Others...' entry
Laurent Montel's avatar
Laurent Montel committed
54
        mTypeList.insert(mTypeList.at(mTypeList.count() - 1), static_cast<int>(type));
Laurent Montel's avatar
Laurent Montel committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }

    mType = type;
    update();
}

KContacts::Address::Type SelectAddressTypeComboBox::type() const
{
    return mType;
}

void SelectAddressTypeComboBox::update()
{
    bool blocked = signalsBlocked();
    blockSignals(true);

    clear();
72 73
    const int numberOfType(mTypeList.count());
    for (int i = 0; i < numberOfType; ++i) {
Laurent Montel's avatar
Laurent Montel committed
74 75
        if (mTypeList.at(i) == -1) {     // "Other..." entry
            addItem(i18nc("@item:inlistbox Category of contact info field", "Other..."));
76 77
        } else if (mTypeList.at(i) == -2) {
            addItem(i18nc("@item:inlistbox Category of contact info field", "New Address Type"));
Laurent Montel's avatar
Laurent Montel committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91
        } else {
            addItem(KContacts::Address::typeLabel(KContacts::Address::Type(mTypeList.at(i))));
        }
    }

    setCurrentIndex(mLastSelected = mTypeList.indexOf(mType));

    blockSignals(blocked);
}

void SelectAddressTypeComboBox::selected(int pos)
{
    if (mTypeList.at(pos) == -1) {
        otherSelected();
92 93
    } else if (mTypeList.at(pos) == -2) {
        return;
Laurent Montel's avatar
Laurent Montel committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    } else {
        mType = KContacts::Address::Type(mTypeList.at(pos));
        mLastSelected = pos;
    }
}

void SelectAddressTypeComboBox::otherSelected()
{
    AutoQPointer<AddressTypeDialog> dlg = new AddressTypeDialog(mType, this);
    if (dlg->exec()) {
        mType = dlg->type();
        if (!mTypeList.contains(mType)) {
            mTypeList.insert(mTypeList.at(mTypeList.count() - 1), mType);
        }
    } else {
        setType(KContacts::Address::Type(mTypeList.at(mLastSelected)));
    }
    update();
}