11#include <kwidgetitemdelegate.h>
12#include <klocalizedstring.h>
18#include <QAbstractTableModel>
26#include <QStandardPaths>
47 : KWidgetItemDelegate(itemView, dlg)
55 QWidget *page =
new QWidget;
56 QHBoxLayout *layout =
new QHBoxLayout(page);
58 QCheckBox *checkBox =
new QCheckBox;
59 checkBox->setProperty(
"fileitem", index.data());
61 connect(checkBox, SIGNAL(toggled(
bool)),
m_parent, SLOT(toggleFileItem(
bool)));
62 QLabel *thumbnail =
new QLabel;
63 QLabel *filename =
new QLabel;
64 QLabel *dateModified =
new QLabel;
66 layout->addWidget(checkBox);
67 layout->addWidget(thumbnail);
68 layout->addWidget(filename);
69 layout->addWidget(dateModified);
71 page->setFixedSize(600, 200);
76#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
78 const QStyleOptionViewItem &option,
79 const QPersistentModelIndex &index)
const override
82 const QStyleOptionViewItem &option,
83 const QPersistentModelIndex &index)
const override
90 QWidget* page= widgets[0];
91 QHBoxLayout* layout = qobject_cast<QHBoxLayout*>(page->layout());
92 QCheckBox *checkBox = qobject_cast<QCheckBox*>(layout->itemAt(0)->widget());
93 QLabel *thumbnail = qobject_cast<QLabel*>(layout->itemAt(1)->widget());
94 QLabel *filename = qobject_cast<QLabel*>(layout->itemAt(2)->widget());
95 QLabel *modified = qobject_cast<QLabel*>(layout->itemAt(3)->widget());
97 checkBox->setChecked(fileItem->
checked);
98 thumbnail->setPixmap(QPixmap::fromImage(fileItem->
thumbnail));
99 filename->setText(fileItem->
name);
100 modified->setText(fileItem->
date);
103 page->setGeometry(option.rect.translated(0, -option.rect.y()));
106 void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &)
const override
109 QStyleOptionViewItem opt = option;
110 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, 0);
113 QSize
sizeHint(
const QStyleOptionViewItem&,
const QModelIndex&)
const override
115 return QSize(600, 200);
126 : QAbstractListModel(parent)
137 Qt::ItemFlags
flags(
const QModelIndex& )
const override
139 Qt::ItemFlags
flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
143 QVariant
data(
const QModelIndex& index,
int role)
const override
145 if (index.isValid() && index.row() <
m_fileItems.size()) {
150 case Qt::DisplayRole:
152 return QVariant::fromValue<void*>((
void*)item);
154 case Qt::SizeHintRole:
155 return QSize(600, 200);
161 bool setData(
const QModelIndex& index,
const QVariant& ,
int role)
override
163 if (index.isValid() && index.row() <
m_fileItems.size()) {
164 if (role == Qt::CheckStateRole) {
177 setCaption(i18nc(
"@title:window",
"Recover Files"));
180 setMinimumSize(650, 500);
181 QWidget *page =
new QWidget(
this);
182 QVBoxLayout *layout =
new QVBoxLayout(page);
183 if (filenames.size() == 1) {
184 layout->addWidget(
new QLabel(i18n(
"The following autosave file can be recovered:")));
187 layout->addWidget(
new QLabel(i18n(
"The following autosave files can be recovered:")));
196 Q_FOREACH (
const QString &filename, filenames) {
199 file->name = filename;
206 QString thumbnailPath = QLatin1String(
"Thumbnails/thumbnail.png");
207 QString previewPath = QLatin1String(
"preview.png");
208 bool thumbnailExists = store->
hasFile(thumbnailPath);
209 bool previewExists = store->
hasFile(previewPath);
210 QString pathToUse = thumbnailExists ? thumbnailPath : (previewExists ? previewPath :
"");
212 if (!pathToUse.isEmpty() && store->
open(pathToUse)) {
214 QByteArray bytes = store->
read(store->
size());
217 img.loadFromData(bytes);
218 file->thumbnail = img.scaled(QSize(200,200), Qt::KeepAspectRatio, Qt::SmoothTransformation);
225 QDateTime date = QFileInfo(path).lastModified();
226 file->date =
"(" + QLocale::system().toString(date, QLocale::ShortFormat) +
")";
228 fileItems.append(file);
235 connect(scroller, &QScroller::stateChanged,
this, [&](QScroller::State state) {
241 layout->addWidget(
new QLabel(i18n(
"If you select Cancel, all recoverable files will be kept.\nIf you press OK, selected files will be recovered, the unselected files discarded.")));
244 setAttribute(Qt::WA_DeleteOnClose,
false);
268 files << fileItem->
name;
278 return QDir::tempPath();
279#elif defined(Q_OS_ANDROID)
280 QString path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation).append(
"/krita-backup");
281 if (!QDir(path).exists()) {
288 return QDir::homePath();
295 QVariant
v = sender()->property(
"fileitem") ;
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void updateItemWidgets(const QList< QWidget * > &widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &) const override
FileItemDelegate(QAbstractItemView *itemView, KisAutoSaveRecoveryDialog *dlg)
KisAutoSaveRecoveryDialog * m_parent
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override
QList< QWidget * > createItemWidgets(const QModelIndex &index) const override
bool setData(const QModelIndex &index, const QVariant &, int role) override
QList< FileItem * > m_fileItems
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &) const override
FileItemModel(QList< FileItem * > fileItems, QObject *parent)
Qt::ItemFlags flags(const QModelIndex &) const override
~FileItemModel() override
static QString autoSaveLocation()
KisAutoSaveRecoveryDialog(const QStringList &filenames, QWidget *parent=0)
~KisAutoSaveRecoveryDialog() override
QStringList recoverableFiles()
void toggleFileItem(bool toggle)
A dialog base class with standard buttons and predefined layouts.
void setMainWidget(QWidget *widget)
void setButtonText(ButtonCode id, const QString &text)
virtual void setCaption(const QString &caption)
void setButtons(ButtonCodes buttonMask)
@ Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted)
@ User1
Show User defined button 1.
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
static KoStore * createStore(const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
bool hasFile(const QString &fileName) const
bool open(const QString &name)
QByteArray read(qint64 max)