File indexing completed on 2024-12-08 03:59:52

0001 /*
0002 This file is part of KDE 
0003 
0004   Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org)
0005 
0006 Permission is hereby granted, free of charge, to any person obtaining a copy
0007 of this software and associated documentation files (the "Software"), to deal
0008 in the Software without restriction, including without limitation the rights
0009 to use, copy, modify, merge, publish, distribute, and/or sell
0010 copies of the Software, and to permit persons to whom the Software is
0011 furnished to do so, subject to the following conditions:
0012 
0013 The above copyright notice and this permission notice shall be included in
0014 all copies or substantial portions of the Software.
0015 
0016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 */
0023 
0024 #include "kcolorchooser_version.h"
0025 
0026 #include <KAboutData>
0027 #include <KLocalizedString>
0028 #include <KHelpMenu>
0029 
0030 #include <QApplication>
0031 #include <QClipboard>
0032 #include <QColorDialog>
0033 #include <QCommandLineParser>
0034 #include <QDialogButtonBox>
0035 #include <QMenu>
0036 #include <QMimeData>
0037 #include <QPushButton>
0038 
0039 #include <iostream>
0040 
0041 int main(int argc, char *argv[])
0042 {
0043     QApplication app(argc, argv);
0044 
0045     KLocalizedString::setApplicationDomain("kcolorchooser");
0046 
0047     KAboutData aboutData(QStringLiteral("kcolorchooser"),
0048                          i18n("KColorChooser"),
0049                          QStringLiteral(KCOLORCHOOSER_VERSION_STRING),
0050                          i18n("KDE Color Chooser"),
0051                          KAboutLicense::BSDL,
0052                          i18n("(c) 2000, Waldo Bastian"));
0053     aboutData.addAuthor(i18n("Waldo Bastian"), QString(), QStringLiteral("bastian@kde.org"));
0054     aboutData.addAuthor(i18n("Hugo Parente Lima"),i18n("KF5 port"), QStringLiteral("hugo.pl@gmail.com"));
0055     aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"), i18nc("EMAIL OF TRANSLATORS", "Your emails"));
0056     KAboutData::setApplicationData(aboutData);
0057 
0058     QCommandLineParser parser;
0059     parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
0060     aboutData.setupCommandLine(&parser);
0061     QCommandLineOption print(QStringLiteral("print"), i18n("Print the selected color to stdout."));
0062     parser.addOption(print);
0063     QCommandLineOption color(QStringLiteral("color"), i18n("Set initially selected color."), QStringLiteral("color"));
0064     parser.addOption(color);
0065     parser.process(app);
0066     aboutData.processCommandLine(&parser);
0067 
0068     QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("kcolorchooser")));
0069 
0070     QColorDialog dlg;
0071     dlg.setOption(QColorDialog::DontUseNativeDialog);
0072     QDialogButtonBox *box = dlg.findChild<QDialogButtonBox*>();
0073     if (!box) {
0074         return 1;
0075     }
0076 
0077     box->addButton(QDialogButtonBox::Help);
0078 
0079     KHelpMenu *help = new KHelpMenu(&dlg, aboutData, false);
0080     help->menu();
0081     delete help->action(KHelpMenu::menuHelpContents);
0082     QObject::connect(box, &QDialogButtonBox::helpRequested, [=] () {
0083         QPushButton *button = box->button(QDialogButtonBox::Help);
0084         QPoint pos = button->pos();
0085         pos.ry() += button->height();
0086         pos = box->mapToGlobal(pos);
0087         help->menu()->exec(pos);
0088     });
0089 
0090     if (parser.isSet(color)) {
0091         dlg.setCurrentColor(QColor(parser.value(color)));
0092     } else {
0093         const QMimeData *mimeData = QApplication::clipboard()->mimeData(QClipboard::Clipboard);
0094         if (mimeData) {
0095             QColor clipboardColor = mimeData->colorData().value<QColor>();
0096             if (clipboardColor.isValid()) {
0097                 dlg.setCurrentColor(clipboardColor);
0098             }
0099         }
0100     }
0101 
0102     dlg.show();
0103     app.exec();
0104 
0105     const  QColor c = dlg.currentColor();
0106     if (parser.isSet(print) && c.isValid()) {
0107         std::cout << c.name().toUtf8().constData() << std::endl;
0108     }
0109 }