mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2058125 [Moving actions to the top causes one to disappear and the other to loop INSTEAD of stopping](https://bugs.launchpad.net/calibre/+bug/2058125)
This commit is contained in:
parent
f64181b09d
commit
0f0ac817c7
@ -5,7 +5,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
from qt.core import QAbstractListModel, QIcon, QItemSelectionModel, Qt
|
from qt.core import QAbstractItemView, QAbstractListModel, QIcon, QItemSelectionModel, Qt
|
||||||
|
|
||||||
from calibre import force_unicode
|
from calibre import force_unicode
|
||||||
from calibre.gui2 import error_dialog, gprefs, warning_dialog
|
from calibre.gui2 import error_dialog, gprefs, warning_dialog
|
||||||
@ -163,25 +163,29 @@ class CurrentModel(BaseModel):
|
|||||||
self.key = key
|
self.key = key
|
||||||
self.gui = gui
|
self.gui = gui
|
||||||
|
|
||||||
def move(self, idx, delta):
|
def move_single(self, idx, delta):
|
||||||
row = idx.row()
|
row = idx.row()
|
||||||
nrow = (row + delta + len(self._data)) % len(self._data)
|
nrow = (row + delta + len(self._data)) % len(self._data)
|
||||||
if nrow < 0 or nrow >= len(self._data):
|
if row + delta < 0:
|
||||||
return
|
x = self._data.pop(row)
|
||||||
t = self._data[row]
|
self._data.append(x)
|
||||||
self._data[row] = self._data[nrow]
|
elif row + delta >= len(self._data):
|
||||||
self._data[nrow] = t
|
x = self._data.pop(row)
|
||||||
ni = self.index(nrow)
|
self._data.insert(0, x)
|
||||||
self.dataChanged.emit(idx, idx)
|
else:
|
||||||
self.dataChanged.emit(ni, ni)
|
self._data[row], self._data[nrow] = self._data[nrow], self._data[row]
|
||||||
return ni
|
|
||||||
|
|
||||||
def move_many(self, indices, delta):
|
def move_many(self, indices, delta):
|
||||||
|
rows = [i.row() for i in indices]
|
||||||
|
items = [self._data[x.row()] for x in indices]
|
||||||
|
self.beginResetModel()
|
||||||
indices = sorted(indices, key=lambda i: i.row(), reverse=delta > 0)
|
indices = sorted(indices, key=lambda i: i.row(), reverse=delta > 0)
|
||||||
|
for item in items:
|
||||||
|
self.move_single(self.index(self._data.index(item)), delta)
|
||||||
|
self.endResetModel()
|
||||||
ans = {}
|
ans = {}
|
||||||
for idx in indices:
|
for item, row in zip(items, rows):
|
||||||
ni = self.move(idx, delta)
|
ans[row] = self.index(self._data.index(item))
|
||||||
ans[idx.row()] = ni
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def add(self, names):
|
def add(self, names):
|
||||||
@ -354,11 +358,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
m = self.current_actions.model()
|
m = self.current_actions.model()
|
||||||
idx_map = m.move_many(x, delta)
|
idx_map = m.move_many(x, delta)
|
||||||
newci = idx_map.get(i)
|
newci = idx_map.get(i)
|
||||||
if newci is not None:
|
|
||||||
sm.setCurrentIndex(newci, QItemSelectionModel.SelectionFlag.ClearAndSelect)
|
|
||||||
sm.clear()
|
sm.clear()
|
||||||
for idx in idx_map.values():
|
for idx in idx_map.values():
|
||||||
sm.select(idx, QItemSelectionModel.SelectionFlag.Select)
|
sm.select(idx, QItemSelectionModel.SelectionFlag.Select)
|
||||||
|
if newci is not None:
|
||||||
|
sm.setCurrentIndex(newci, QItemSelectionModel.SelectionFlag.SelectCurrent)
|
||||||
|
if newci is not None:
|
||||||
|
self.current_actions.scrollTo(newci, QAbstractItemView.ScrollHint.EnsureVisible)
|
||||||
self.changed_signal.emit()
|
self.changed_signal.emit()
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user