logactivitiesdialog.cpp 5.05 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (C) 2017-2019 Montel Laurent <montel@kde.org>
3
4

  This program is free software; you can redistribute it and/or modify it
5
6
7
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
8
9
10
11
12
13
14
15
16
17
18
19

  This program 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
  General Public License for more details.

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

#include "logactivitiesdialog.h"
Laurent Montel's avatar
Laurent Montel committed
20
#include "logactivitieswidget.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "logactivitiesmanager.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "logactivitiespurposemenuwidget.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include <PimCommon/PimUtil>
Laurent Montel's avatar
Laurent Montel committed
24
25
26
#include <KLocalizedString>
#include <KConfigGroup>
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
27
28
#include <QVBoxLayout>
#include <QDialogButtonBox>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
30
#include <QCheckBox>
31
32
33
34
35
36

using namespace PimCommon;

LogActivitiesDialog::LogActivitiesDialog(QWidget *parent)
    : QDialog(parent)
{
37
    setWindowTitle(i18nc("@title:window", "Log activities"));
Laurent Montel's avatar
Laurent Montel committed
38
39
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setObjectName(QStringLiteral("mainLayout"));
Laurent Montel's avatar
Laurent Montel committed
40
41
42
43
44

    mLogWidget = new LogActivitiesWidget(this);
    mLogWidget->setObjectName(QStringLiteral("logwidget"));
    mainLayout->addWidget(mLogWidget);

Laurent Montel's avatar
Laurent Montel committed
45
46
47
48
49
    mEnableLogActivities = new QCheckBox(i18n("Log activities"), this);
    mEnableLogActivities->setObjectName(QStringLiteral("enablelogactivities"));
    mainLayout->addWidget(mEnableLogActivities);
    connect(mEnableLogActivities, &QCheckBox::toggled, this, &LogActivitiesDialog::slotEnableLogActivities);

Laurent Montel's avatar
Laurent Montel committed
50
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close | QDialogButtonBox::Save, this);
Laurent Montel's avatar
Laurent Montel committed
51
    buttonBox->setObjectName(QStringLiteral("buttonbox"));
Laurent Montel's avatar
Laurent Montel committed
52
53
54
55
56

    mClearButton = new QPushButton(i18n("Clear"), this);
    mClearButton->setObjectName(QStringLiteral("clearbutton"));
    buttonBox->addButton(mClearButton, QDialogButtonBox::ActionRole);
    connect(mClearButton, &QPushButton::clicked, this, &LogActivitiesDialog::slotClear);
Laurent Montel's avatar
Laurent Montel committed
57
58
59
    mSaveButton = buttonBox->button(QDialogButtonBox::Save);
    mSaveButton->setObjectName(QStringLiteral("savebutton"));
    mSaveButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
60
61

    LogactivitiesPurposeMenuWidget *purposeMenu = new LogactivitiesPurposeMenuWidget(this, this);
Laurent Montel's avatar
Laurent Montel committed
62
63
64
65
66
67
    mShareButton = new QPushButton(i18n("Share..."), this);
    mShareButton->setMenu(purposeMenu->menu());
    mShareButton->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
    purposeMenu->setEditorWidget(mLogWidget->editor());
    buttonBox->addButton(mShareButton, QDialogButtonBox::ActionRole);
    mShareButton->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
68

Laurent Montel's avatar
Laurent Montel committed
69
    connect(mSaveButton, &QPushButton::clicked, this, &LogActivitiesDialog::slotSave);
Laurent Montel's avatar
Laurent Montel committed
70

Laurent Montel's avatar
Laurent Montel committed
71
    mainLayout->addWidget(buttonBox);
Laurent Montel's avatar
Laurent Montel committed
72
    connect(buttonBox, &QDialogButtonBox::rejected, this, &LogActivitiesDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
73
    readConfig();
Laurent Montel's avatar
Laurent Montel committed
74
75
76

    connect(PimCommon::LogActivitiesManager::self(), &LogActivitiesManager::logEntryAdded, this, &LogActivitiesDialog::slotLogEntryAdded);
    connect(PimCommon::LogActivitiesManager::self(), &LogActivitiesManager::logEntryCleared, this, &LogActivitiesDialog::slotLogEntryCleared);
Laurent Montel's avatar
Laurent Montel committed
77
    connect(mLogWidget, &LogActivitiesWidget::textChanged, this, &LogActivitiesDialog::slotActivityTextChanged);
Laurent Montel's avatar
Laurent Montel committed
78
    mEnableLogActivities->setChecked(PimCommon::LogActivitiesManager::self()->enableLogActivities());
79
80
81
82
}

LogActivitiesDialog::~LogActivitiesDialog()
{
Laurent Montel's avatar
Laurent Montel committed
83
    disconnect(mLogWidget, &LogActivitiesWidget::textChanged, this, &LogActivitiesDialog::slotActivityTextChanged);
Laurent Montel's avatar
Laurent Montel committed
84
    writeConfig();
85
}
Laurent Montel's avatar
Laurent Montel committed
86

Laurent Montel's avatar
Laurent Montel committed
87
88
89
void LogActivitiesDialog::slotActivityTextChanged(bool changed)
{
    mSaveButton->setEnabled(changed);
Laurent Montel's avatar
Laurent Montel committed
90
    mShareButton->setEnabled(changed);
Laurent Montel's avatar
Laurent Montel committed
91
92
}

Laurent Montel's avatar
Laurent Montel committed
93
94
void LogActivitiesDialog::slotSave()
{
Laurent Montel's avatar
Laurent Montel committed
95
96
97
    const QString filter = i18n("All Files (*)");
    PimCommon::Util::saveTextAs(PimCommon::LogActivitiesManager::self()->log(), filter, this, QUrl(),
                                i18nc("@title:window", "Save Log"));
Laurent Montel's avatar
Laurent Montel committed
98
99
}

Laurent Montel's avatar
Laurent Montel committed
100
101
102
103
104
void LogActivitiesDialog::slotEnableLogActivities(bool state)
{
    PimCommon::LogActivitiesManager::self()->setEnableLogActivities(state);
}

Laurent Montel's avatar
Laurent Montel committed
105
106
107
108
void LogActivitiesDialog::setLog(const QString &str)
{
    mLogWidget->setLog(str);
}
Laurent Montel's avatar
Laurent Montel committed
109

Laurent Montel's avatar
Laurent Montel committed
110
111
112
113
114
115
116
117
118
119
void LogActivitiesDialog::slotLogEntryAdded(const QString &entry)
{
    mLogWidget->addLogEntry(entry);
}

void LogActivitiesDialog::slotLogEntryCleared()
{
    mLogWidget->clear();
}

Laurent Montel's avatar
Laurent Montel committed
120
121
122
123
124
void LogActivitiesDialog::slotClear()
{
    mLogWidget->clear();
    Q_EMIT logCleared();
}
Laurent Montel's avatar
Laurent Montel committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

void LogActivitiesDialog::readConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "LogActivitiesDialog");
    const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
    if (sizeDialog.isValid()) {
        resize(sizeDialog);
    }
}

void LogActivitiesDialog::writeConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "LogActivitiesDialog");
    group.writeEntry("Size", size());
}