Skip to content

Commit

Permalink
Fix shortcut conflict when one is a default (#1989)
Browse files Browse the repository at this point in the history
* Fix config checking on startup

Signed-off-by: Haris Gušić <[email protected]>

* Handle conflicts when one shortcut is a default

Signed-off-by: Haris Gušić <[email protected]>

* Fix bugs

Signed-off-by: Haris Gušić <[email protected]>

* Refactor slot in ShortcutsWidget for consistency

Signed-off-by: Haris Gušić <[email protected]>

* Update shortcut table on config file change

Signed-off-by: Haris Gušić <[email protected]>

* Fix bounded int bug in config

Signed-off-by: Haris Gušić <[email protected]>

* Revert changes manually

Signed-off-by: Haris Gušić <[email protected]>

* Reimplement ConfigHandler::shortcut and setShortcut

Signed-off-by: Haris Gušić <[email protected]>

* Handle Return/Numpad-Enter equivalently

Signed-off-by: Haris Gušić <[email protected]>

* Re-enable user-configured Imgur upload shortcut

Signed-off-by: Haris Gušić <[email protected]>

* Document copyAndCloseAfterUpload config option

Signed-off-by: Haris Gušić <[email protected]>
  • Loading branch information
veracioux authored Oct 24, 2021
1 parent 08e532f commit a487fb0
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 142 deletions.
3 changes: 3 additions & 0 deletions flameshot.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
;; Copy path to image after save (bool)
;copyPathAfterSave=false
;
;; On successful upload, close the dialog and copy URL to clipboard.
;copyAndCloseAfterUpload=true
;
;; Anti-aliasing image when zoom the pinned image (bool)
;antialiasingPinZoom=true
;
Expand Down
2 changes: 1 addition & 1 deletion src/config/configwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void ConfigWindow::initErrorIndicator(QWidget* tab, QWidget* widget)

qApp->processEvents();
QPoint center = dialog.geometry().center();
QRect dialogRect(0, 0, 400, 400);
QRect dialogRect(0, 0, 600, 400);
dialogRect.moveCenter(center);
dialog.setGeometry(dialogRect);

Expand Down
77 changes: 35 additions & 42 deletions src/config/shortcutswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ ShortcutsWidget::ShortcutsWidget(QWidget* parent)
m_layout = new QVBoxLayout(this);
m_layout->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

initShortcuts();
initInfoTable();
connect(ConfigHandler::getInstance(),
&ConfigHandler::fileChanged,
this,
&ShortcutsWidget::populateInfoTable);
show();
}

const QList<QStringList>& ShortcutsWidget::shortcuts()
{
return m_shortcuts;
}

void ShortcutsWidget::initInfoTable()
{
m_table = new QTableWidget(this);
Expand All @@ -56,7 +54,6 @@ void ShortcutsWidget::initInfoTable()
m_layout->addWidget(m_table);

m_table->setColumnCount(2);
m_table->setRowCount(m_shortcuts.size());
m_table->setSelectionMode(QAbstractItemView::NoSelection);
m_table->setFocusPolicy(Qt::NoFocus);
m_table->verticalHeader()->hide();
Expand All @@ -66,21 +63,35 @@ void ShortcutsWidget::initInfoTable()
names << tr("Description") << tr("Key");
m_table->setHorizontalHeaderLabels(names);
connect(m_table,
SIGNAL(cellClicked(int, int)),
&QTableWidget::cellClicked,
this,
SLOT(slotShortcutCellClicked(int, int)));
&ShortcutsWidget::onShortcutCellClicked);

// populate with dynamic data
populateInfoTable();

// adjust size
m_table->horizontalHeader()->setMinimumSectionSize(200);
m_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_table->horizontalHeader()->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
m_table->resizeColumnsToContents();
m_table->resizeRowsToContents();
}

void ShortcutsWidget::populateInfoTable()
{
loadShortcuts();
m_table->setRowCount(m_shortcuts.size());

// add content
for (int i = 0; i < shortcuts().size(); ++i) {
for (int i = 0; i < m_shortcuts.size(); ++i) {
const auto current_shortcut = m_shortcuts.at(i);
const auto identifier = current_shortcut.at(0);
const auto description = current_shortcut.at(1);
const auto default_key_sequence = current_shortcut.at(2);
const auto key_sequence = current_shortcut.at(2);
m_table->setItem(i, 0, new QTableWidgetItem(description));

const auto key_sequence = identifier.isEmpty()
? default_key_sequence
: m_config.shortcut(identifier);
#if defined(Q_OS_MACOS)
QTableWidgetItem* item =
new QTableWidgetItem(nativeOSHotKeyText(key_sequence));
Expand All @@ -106,17 +117,9 @@ void ShortcutsWidget::initInfoTable()
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
}
}

// adjust size
m_table->resizeColumnsToContents();
m_table->resizeRowsToContents();
m_table->horizontalHeader()->setMinimumSectionSize(200);
m_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_table->horizontalHeader()->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
}

void ShortcutsWidget::slotShortcutCellClicked(int row, int col)
void ShortcutsWidget::onShortcutCellClicked(int row, int col)
{
if (col == 1) {
// Ignore non-changable shortcuts
Expand All @@ -140,38 +143,27 @@ void ShortcutsWidget::slotShortcutCellClicked(int row, int col)
shortcutValue = QKeySequence("");
}
#endif

if (m_config.setShortcut(shortcutName, shortcutValue.toString())) {
#if defined(Q_OS_MACOS)
QTableWidgetItem* item = new QTableWidgetItem(
nativeOSHotKeyText(shortcutValue.toString()));
#else
QTableWidgetItem* item =
new QTableWidgetItem(shortcutValue.toString());
#endif
item->setTextAlignment(Qt::AlignCenter);
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
m_table->setItem(row, col, item);
populateInfoTable();
}
}
delete setShortcutDialog;
}
}

void ShortcutsWidget::initShortcuts()
void ShortcutsWidget::loadShortcuts()
{
m_shortcuts.clear();
auto buttonTypes = CaptureToolButton::getIterableButtonTypes();

// get shortcuts names from capture buttons
for (const CaptureTool::Type& t : buttonTypes) {
CaptureTool* tool = ToolFactory().CreateTool(t);
QString shortcutName = QVariant::fromValue(t).toString();
if (t != CaptureTool::TYPE_IMAGEUPLOADER) {
appendShortcut(shortcutName, tool->description());
if (shortcutName == "TYPE_COPY")
m_shortcuts << (QStringList() << "" << tool->description()
<< "Left Double-click");
}
appendShortcut(shortcutName, tool->description());
if (shortcutName == "TYPE_COPY")
m_shortcuts << (QStringList() << "" << tool->description()
<< "Left Double-click");
delete tool;
}

Expand Down Expand Up @@ -219,10 +211,11 @@ void ShortcutsWidget::initShortcuts()
void ShortcutsWidget::appendShortcut(const QString& shortcutName,
const QString& description)
{
QString shortcut = ConfigHandler().shortcut(shortcutName);
m_shortcuts << (QStringList()
<< shortcutName
<< QObject::tr(description.toStdString().c_str())
<< ConfigHandler().shortcut(shortcutName));
<< shortcut.replace("Return", "Enter"));
}

#if defined(Q_OS_MACOS)
Expand Down
6 changes: 3 additions & 3 deletions src/config/shortcutswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ShortcutsWidget : public QWidget
Q_OBJECT
public:
explicit ShortcutsWidget(QWidget* parent = nullptr);
const QList<QStringList>& shortcuts();

private:
void initInfoTable();
Expand All @@ -28,7 +27,8 @@ class ShortcutsWidget : public QWidget
#endif

private slots:
void slotShortcutCellClicked(int, int);
void populateInfoTable();
void onShortcutCellClicked(int, int);

private:
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
Expand All @@ -40,7 +40,7 @@ private slots:
QVBoxLayout* m_layout;
QList<QStringList> m_shortcuts;

void initShortcuts();
void loadShortcuts();
void appendShortcut(const QString& shortcutName,
const QString& description);
};
Expand Down
Loading

0 comments on commit a487fb0

Please sign in to comment.