todoviewquicksearch.cpp 4.69 KB
Newer Older
1
2
3
/*
  This file is part of KOrganizer.

4
5
6
7
8
  SPDX-FileCopyrightText: 2004 Till Adam <adam@kde.org>
  SPDX-FileCopyrightText: 2005 Rafal Rzepecki <divide@users.sourceforge.net>
  SPDX-FileCopyrightText: 2008 Thomas Thrainer <tom_t@gmx.at>

  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
9
10
11
12
13
*/

#include "todoviewquicksearch.h"
#include <CalendarSupport/KCalPrefs>

14
#include <AkonadiWidgets/TagSelectionComboBox>
15
16
17
18
19
20
#include <CalendarSupport/CategoryConfig>

#include <Libkdepim/KCheckComboBox>

#include <CalendarSupport/CategoryHierarchyReader>

21
#include <KCalendarCore/CalFilter>
22
23

#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QLineEdit>
25
26
27

#include <QHBoxLayout>

Laurent Montel's avatar
Laurent Montel committed
28
29
30
TodoViewQuickSearch::TodoViewQuickSearch(const Akonadi::ETMCalendar::Ptr &calendar, QWidget *parent)
    : QWidget(parent)
    , mCalendar(calendar)
31
{
32
    auto layout = new QHBoxLayout(this);
33
34
35
36
    // no special margin because it is added by the view
    layout->setContentsMargins(0, 0, 0, 0);

    mSearchLine = new QLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
37
38
    mSearchLine->setToolTip(i18nc("@info:tooltip", "Filter on matching summaries"));
    mSearchLine->setWhatsThis(i18nc("@info:whatsthis", "Enter text here to filter the to-dos that are shown by matching summaries."));
39
    mSearchLine->setPlaceholderText(i18nc("@label in QuickSearchLine", "Search Summaries..."));
40
41
42
43
44
    mSearchLine->setClearButtonEnabled(true);
    connect(mSearchLine, &QLineEdit::textChanged, this, &TodoViewQuickSearch::searchTextChanged);

    layout->addWidget(mSearchLine, 3);

45
46
    mCategoryCombo = new Akonadi::TagSelectionComboBox(this);
    mCategoryCombo->setCheckable(true);
Laurent Montel's avatar
Laurent Montel committed
47
48
49
50
    mCategoryCombo->setToolTip(i18nc("@info:tooltip", "Filter on these categories"));
    mCategoryCombo->setWhatsThis(i18nc("@info:whatsthis",
                                       "Use this combobox to filter the to-dos that are shown by "
                                       "a list of selected categories."));
51
    const QString defaultText = i18nc("@item:inlistbox", "Select Categories");
52
    mCategoryCombo->lineEdit()->setPlaceholderText(defaultText);
53

Laurent Montel's avatar
Laurent Montel committed
54
55
56
    connect(mCategoryCombo, &Akonadi::TagSelectionComboBox::selectionChanged, this, [this]() {
        Q_EMIT filterCategoryChanged(mCategoryCombo->selectionNames());
    });
57
58
59
60
61
62
63
64
65
66
67

    layout->addWidget(mCategoryCombo, 1);

    {
        // Make the combo big enough so that "Select Categories" fits.
        QFontMetrics fm = mCategoryCombo->lineEdit()->fontMetrics();

        // QLineEdit::sizeHint() returns a nice size to fit 17 'x' chars.
        const int currentPreferedWidth = mCategoryCombo->lineEdit()->sizeHint().width();

        // Calculate a nice size for "Select Categories"
Laurent Montel's avatar
Laurent Montel committed
68
        const int newPreferedWidth = currentPreferedWidth - fm.boundingRect(QLatin1Char('x')).width() * 17 + fm.boundingRect(defaultText).width();
69
70
71
72
73
74

        const int pixelsToAdd = newPreferedWidth - mCategoryCombo->lineEdit()->width();
        mCategoryCombo->setMinimumWidth(mCategoryCombo->width() + pixelsToAdd);
    }

    mPriorityCombo = new KPIM::KCheckComboBox(this);
Laurent Montel's avatar
Laurent Montel committed
75
76
77
78
    mPriorityCombo->setToolTip(i18nc("@info:tooltip", "Filter on these priorities"));
    mPriorityCombo->setWhatsThis(i18nc("@info:whatsthis",
                                       "Use this combobox to filter the to-dos that are shown by "
                                       "a list of selected priorities."));
79
    mPriorityCombo->lineEdit()->setPlaceholderText(i18nc("@item:inlistbox", "Select Priority"));
Laurent Montel's avatar
Laurent Montel committed
80
81
82
    connect(mPriorityCombo, &KPIM::KCheckComboBox::checkedItemsChanged, this, [this]() {
        Q_EMIT filterPriorityChanged(mPriorityCombo->checkedItems(Qt::UserRole));
    });
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

    layout->addWidget(mPriorityCombo, 1);
    fillPriorities();
}

void TodoViewQuickSearch::setCalendar(const Akonadi::ETMCalendar::Ptr &calendar)
{
    if (calendar != mCalendar) {
        mCalendar = calendar;
    }
}

void TodoViewQuickSearch::reset()
{
    mSearchLine->clear();
    mCategoryCombo->setCurrentIndex(0);
    mPriorityCombo->setCurrentIndex(0);
}

void TodoViewQuickSearch::fillPriorities()
{
    QStringList priorityValues;
    priorityValues.append(i18nc("@action:inmenu priority is unspecified", "unspecified"));
    priorityValues.append(i18nc("@action:inmenu highest priority", "%1 (highest)", 1));
    for (int p = 2; p < 10; ++p) {
        if (p == 5) {
            priorityValues.append(i18nc("@action:inmenu medium priority", "%1 (medium)", p));
        } else if (p == 9) {
            priorityValues.append(i18nc("@action:inmenu lowest priority", "%1 (lowest)", p));
        } else {
            priorityValues.append(i18nc("@action:inmenu", "%1", p));
        }
    }
    // TODO: Using the same method as for categories to fill mPriorityCombo
    CalendarSupport::CategoryHierarchyReaderQComboBox(mPriorityCombo).read(priorityValues);
}