File indexing completed on 2024-05-05 04:34:52
0001 /* SPDX-FileCopyrightText: 2022 Noah Davis <noahadvs@gmail.com> 0002 * SPDX-License-Identifier: LGPL-2.0-or-later 0003 */ 0004 0005 #include "CaptureModeModel.h" 0006 #include "ShortcutActions.h" 0007 0008 #include <KGlobalAccel> 0009 #include <KLocalizedString> 0010 0011 #include <QApplication> 0012 #include <qnamespace.h> 0013 #include <utility> 0014 0015 using namespace Qt::StringLiterals; 0016 0017 static QString actionShortcutsToString(QAction *action) 0018 { 0019 QString value; 0020 if (!KGlobalAccel::self()->hasShortcut(action)) { 0021 return value; 0022 } 0023 0024 const auto &shortcuts = KGlobalAccel::self()->shortcut(action); 0025 for (int i = 0; i < shortcuts.length(); ++i) { 0026 if (i > 0) { 0027 value += u", "; 0028 } 0029 value.append(shortcuts[i].toString(QKeySequence::NativeText)); 0030 } 0031 return value; 0032 } 0033 0034 CaptureModeModel::CaptureModeModel(ImagePlatform::GrabModes grabModes, QObject *parent) 0035 : QAbstractListModel(parent) 0036 { 0037 m_roleNames[CaptureModeRole] = "captureMode"_ba; 0038 m_roleNames[Qt::DisplayRole] = "display"_ba; 0039 m_roleNames[ShortcutsRole] = "shortcuts"_ba; 0040 setGrabModes(grabModes); 0041 } 0042 0043 QHash<int, QByteArray> CaptureModeModel::roleNames() const 0044 { 0045 return m_roleNames; 0046 } 0047 0048 QVariant CaptureModeModel::data(const QModelIndex &index, int role) const 0049 { 0050 int row = index.row(); 0051 QVariant ret; 0052 if (!checkIndex(index, CheckIndexOption::IndexIsValid)) { 0053 return ret; 0054 } 0055 if (role == CaptureModeRole) { 0056 ret = m_data.at(row).captureMode; 0057 } else if (role == Qt::DisplayRole) { 0058 ret = m_data.at(row).label; 0059 } else if (role == ShortcutsRole) { 0060 ret = m_data.at(row).shortcuts; 0061 } 0062 return ret; 0063 } 0064 0065 int CaptureModeModel::rowCount(const QModelIndex &parent) const 0066 { 0067 Q_UNUSED(parent) 0068 return m_data.size(); 0069 } 0070 0071 int CaptureModeModel::indexOfCaptureMode(CaptureMode captureMode) const 0072 { 0073 int finalIndex = -1; 0074 for (int i = 0; i < m_data.length(); ++i) { 0075 if (m_data[i].captureMode == captureMode) { 0076 finalIndex = i; 0077 break; 0078 } 0079 } 0080 return finalIndex; 0081 } 0082 0083 void CaptureModeModel::setGrabModes(ImagePlatform::GrabModes modes) 0084 { 0085 if (m_grabModes == modes) { 0086 return; 0087 } 0088 m_grabModes = modes; 0089 const int oldCount = m_data.size(); 0090 m_data.clear(); 0091 0092 const bool hasCurrentScreen = m_grabModes.testFlag(ImagePlatform::GrabMode::CurrentScreen); 0093 0094 if (m_grabModes.testFlag(ImagePlatform::GrabMode::PerScreenImageNative)) { 0095 m_data.append({ 0096 CaptureModeModel::RectangularRegion, 0097 i18n("Rectangular Region"), 0098 actionShortcutsToString(ShortcutActions::self()->regionAction()), 0099 }); 0100 } 0101 if (m_grabModes.testFlag(ImagePlatform::GrabMode::AllScreens)) { 0102 m_data.append({ 0103 CaptureModeModel::AllScreens, 0104 hasCurrentScreen ? i18n("All Screens") : i18n("Full Screen"), 0105 actionShortcutsToString(ShortcutActions::self()->fullScreenAction()), 0106 }); 0107 } 0108 if (m_grabModes.testFlag(ImagePlatform::GrabMode::AllScreensScaled)) { 0109 m_data.append({ 0110 CaptureModeModel::AllScreensScaled, 0111 i18n("All Screens (Scaled to same size)"), 0112 }); 0113 } 0114 if (hasCurrentScreen) { 0115 m_data.append({ 0116 CaptureModeModel::CurrentScreen, 0117 i18n("Current Screen"), 0118 actionShortcutsToString(ShortcutActions::self()->currentScreenAction()), 0119 }); 0120 } 0121 if (m_grabModes.testFlag(ImagePlatform::GrabMode::ActiveWindow)) { 0122 m_data.append({ 0123 CaptureModeModel::ActiveWindow, 0124 i18n("Active Window"), 0125 actionShortcutsToString(ShortcutActions::self()->activeWindowAction()), 0126 }); 0127 } 0128 if (m_grabModes.testFlag(ImagePlatform::GrabMode::WindowUnderCursor)) { 0129 m_data.append({ 0130 CaptureModeModel::WindowUnderCursor, 0131 i18n("Window Under Cursor"), 0132 actionShortcutsToString(ShortcutActions::self()->windowUnderCursorAction()), 0133 }); 0134 } 0135 0136 Q_EMIT captureModesChanged(); 0137 if (oldCount != m_data.size()) { 0138 Q_EMIT countChanged(); 0139 } 0140 } 0141 0142 #include "moc_CaptureModeModel.cpp"