File: kcolorchooser.cpp

package info (click to toggle)
kcolorchooser 4%3A21.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 376 kB
  • sloc: xml: 136; cpp: 72; makefile: 4; sh: 1
file content (109 lines) | stat: -rw-r--r-- 4,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
/*
This file is part of KDE 

  Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "kcolorchooser_version.h"

#include <KAboutData>
#include <KLocalizedString>
#include <KHelpMenu>

#include <QApplication>
#include <QClipboard>
#include <QColorDialog>
#include <QCommandLineParser>
#include <QDialogButtonBox>
#include <QMenu>
#include <QMimeData>
#include <QPushButton>

#include <iostream>

static const char description[] = I18N_NOOP("KDE Color Chooser");

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
    QApplication app(argc, argv);

    KLocalizedString::setApplicationDomain("kcolorchooser");

    KAboutData aboutData(QStringLiteral("kcolorchooser"),
                         i18n("KColorChooser"),
                         QStringLiteral(KCOLORCHOOSER_VERSION_STRING),
                         i18n(description),
                         KAboutLicense::BSDL,
                         i18n("(c) 2000, Waldo Bastian"));
    aboutData.addAuthor(i18n("Waldo Bastian"), QString(), QStringLiteral("bastian@kde.org"));
    aboutData.addAuthor(i18n("Hugo Parente Lima"),i18n("KF5 port"), QStringLiteral("hugo.pl@gmail.com"));
    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
    KAboutData::setApplicationData(aboutData);

    QCommandLineParser parser;
    parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
    aboutData.setupCommandLine(&parser);
    QCommandLineOption print(QStringLiteral("print"), i18n("Print the selected color to stdout."));
    parser.addOption(print);
    QCommandLineOption color(QStringLiteral("color"), i18n("Set initially selected color."), QStringLiteral("color"));
    parser.addOption(color);
    parser.process(app);
    aboutData.processCommandLine(&parser);

    QColorDialog dlg;
    dlg.setOption(QColorDialog::DontUseNativeDialog);
    QDialogButtonBox *box = dlg.findChild<QDialogButtonBox*>();
    if (!box) {
        return 1;
    }

    box->addButton(QDialogButtonBox::Help);

    KHelpMenu *help = new KHelpMenu(&dlg, aboutData);
    QObject::connect(box, &QDialogButtonBox::helpRequested, [=] () {
        QPushButton *button = box->button(QDialogButtonBox::Help);
        QPoint pos = button->pos();
        pos.ry() += button->height();
        pos = box->mapToGlobal(pos);
        help->menu()->exec(pos);
    });

    if (parser.isSet(color)) {
        dlg.setCurrentColor(QColor(parser.value(color)));
    } else {
        const QMimeData *mimeData = QApplication::clipboard()->mimeData(QClipboard::Clipboard);
        if (mimeData) {
            QColor clipboardColor = mimeData->colorData().value<QColor>();
            if (clipboardColor.isValid()) {
                dlg.setCurrentColor(clipboardColor);
            }
        }
    }

    dlg.show();
    app.exec();

    const  QColor c = dlg.currentColor();
    if (parser.isSet(print) && c.isValid()) {
        std::cout << c.name().toUtf8().constData() << std::endl;
    }
}