| 1 | #include "savehistorywidget.h" |
| 2 | #include "deprecation.h" |
| 3 | #include <QScrollArea> |
| 4 | #include <QDialogButtonBox> |
| 5 | #include <QSplitter> |
| 6 | |
| 7 | SaveHistoryWidget::SaveHistoryWidget(QString filename, bool _dark, QWidget *parent ):QDialog(parent){ |
| 8 | pvindex=-1; |
| 9 | fileName=filename; |
| 10 | dark=_dark; |
| 11 | dda = new DragDropVersions(filename,dark,parent); |
| 12 | QScrollArea *qsa = new QScrollArea(); |
| 13 | qsa->setWidget(dda); |
| 14 | QScrollArea *preview = new QScrollArea(); |
| 15 | QVBoxLayout *pvla = new QVBoxLayout(); |
| 16 | pvheader = new QLabel("<h3>current file version</h3>"); |
| 17 | if (dark){ |
| 18 | pvheader->setStyleSheet (QString( |
| 19 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #007700, stop: 1 #424242);} " |
| 20 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," "stop: 0 #242424, stop: 1 #007700);border: 2px solid #eeeeee;}")); |
| 21 | }else{ |
| 22 | pvheader->setStyleSheet (QString( |
| 23 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #aaeeaa, stop: 1 #dadbde);} " |
| 24 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," "stop: 0 #ffffff, stop: 1 #aaee00);border: 2px solid #eeeeee;}")); |
| 25 | } |
| 26 | edi = new CodeEditor(); |
| 27 | edi->dark=dark; |
| 28 | QString inspatternfile=(dark)?":ins_res_style_dark.ini":":ins_res_style.ini"; |
| 29 | edi->highlighter->loadHighlightPatterns(inspatternfile); |
| 30 | QFont font; |
| 31 | font.setFamily(FONTFAM); |
| 32 | font.setFixedPitch(true); |
| 33 | font.setPointSize(9); |
| 34 | edi->setFont(font); |
| 35 | edi->setLineWrapMode(QPlainTextEdit::NoWrap); |
| 36 | QFile p(filename); |
| 37 | p.open(QIODevice::ReadOnly|QIODevice::Text); |
| 38 | edi->setPlainText(orginal=p.readAll()); |
| 39 | p.close(); |
| 40 | pvla->addWidget(pvheader); |
| 41 | pvla->addWidget(edi); |
| 42 | edi->setReadOnly ( true); |
| 43 | preview->setLayout(pvla); |
| 44 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Open | QDialogButtonBox::Cancel); |
| 45 | ok = buttonBox->button(QDialogButtonBox::Open); |
| 46 | |
| 47 | ok->setDisabled(true); |
| 48 | connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); |
| 49 | connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); |
| 50 | QHBoxLayout *la = new QHBoxLayout(); |
| 51 | QVBoxLayout *lo = new QVBoxLayout(); |
| 52 | QSplitter *splitter = new QSplitter(parent); |
| 53 | |
| 54 | splitter->addWidget(qsa); |
| 55 | splitter->addWidget(preview); |
| 56 | splitter->setChildrenCollapsible (false); |
| 57 | splitter->setStretchFactor(1, 1); |
| 58 | la->addWidget(splitter); |
| 59 | lo->addLayout(la); |
| 60 | lo->addWidget(buttonBox); |
| 61 | setLayout(lo); |
| 62 | connect(dda,SIGNAL(indexChanged(int)),this,SLOT(updatePreview(int))); |
| 63 | connect(dda->tick,SIGNAL(timeout()), this , SLOT(tock())); |
| 64 | connect(this,SIGNAL(finished(int)), dda->tick,SLOT(stop())); |
| 65 | setWindowTitle(tr("Save History")); |
| 66 | this->setWindowState(Qt::WindowMaximized); |
| 67 | } |
| 68 | |
| 69 | void SaveHistoryWidget::updatePreview(int index){ |
| 70 | if ((index>=0)&&(index<dda->zeiten.size())) { |
| 71 | DragVersion *childle = static_cast<DragVersion *>(sender()); |
| 72 | if (childle){ |
| 73 | if (dark){ |
| 74 | childle->setStyleSheet (QString( |
| 75 | "QLabel{color: #ffffff; font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #242424);} " |
| 76 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 77 | "stop: 0 #424242, stop: 1 %1);border: 0px solid #eeeeee;;}").arg("#007700")); |
| 78 | } else { |
| 79 | childle->setStyleSheet (QString( |
| 80 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #dadbde);} " |
| 81 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 82 | "stop: 0 #ffffff, stop: 1 %1);border: 0px solid #eeeeee;;}").arg("#00eeaa")); |
| 83 | } |
| 84 | } |
| 85 | pvheader->setText(QString("<h3>%1</h3>").arg(dda->zeiten.at(index).toString())); |
| 86 | pvindex=index; |
| 87 | edi->setReadOnly(false); |
| 88 | QString ext=dda->entries.at(index); |
| 89 | ext.remove(QRegularExpression("^\n")); |
| 90 | edi->setPlainText(ext); |
| 91 | edi->diffTo(orginal); |
| 92 | edi->setReadOnly(true); |
| 93 | } |
| 94 | else if (index==-1) { |
| 95 | pvindex=index; |
| 96 | pvheader->setText("<h3>current file version</h3>"); |
| 97 | edi->setReadOnly(false); |
| 98 | QFile p(fileName); |
| 99 | p.open(QIODevice::ReadOnly|QIODevice::Text); |
| 100 | edi->setPlainText(p.readAll()); |
| 101 | edi->diffTo(orginal); |
| 102 | p.close(); |
| 103 | edi->setReadOnly(true); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void SaveHistoryWidget::tock(){ |
| 108 | if ((pvindex!=-1)&&(pvindex<dda->zeiten.size())){ |
| 109 | pvheader->setText(QString("<h3>%1</h3>").arg(dda->zeiten.at(pvindex).toString())); |
| 110 | ok->setDisabled(false); |
| 111 | }else ok->setDisabled(true); |
| 112 | } |
| 113 | |
| 114 | void SaveHistoryWidget::accept(){ |
| 115 | QFile f(fileName); |
| 116 | |
| 117 | f.open(QIODevice::ReadOnly|QIODevice::Text); |
| 118 | QString alterScheis=f.readAll(); |
| 119 | QFileInfo fi(f); |
| 120 | f.close(); |
| 121 | |
| 122 | bool success = f.open(QIODevice::WriteOnly|QIODevice::Text); |
| 123 | if (success){ |
| 124 | f.write(edi->toPlainText().toLatin1()); |
| 125 | f.close(); |
| 126 | QString dn=fileName.section('/',0,-2), |
| 127 | argument=fileName.section('/',-1); |
| 128 | QDir work=QDir(QString("%1/%2saves/").arg(dn).arg(PROGRAM_NAME)); |
| 129 | if (!work.exists ( QString("%1/%2saves/").arg(dn).arg(PROGRAM_NAME))) work.mkdir (QString("%1/%2saves/").arg(dn).arg(PROGRAM_NAME)); |
| 130 | QString saveHistName=QString("%1/%2saves/SAVEHIST").arg(dn).arg(PROGRAM_NAME); |
| 131 | QFile sahi(saveHistName); |
| 132 | sahi.open(QIODevice::Append|QIODevice::Text); |
| 133 | sahi.write(QString("Entry|@|%1|@|%2|@|\n").arg(fi.lastModified().toString(Qt::ISODate)).arg(argument).toLatin1()); |
| 134 | sahi.write(alterScheis.toLatin1()); |
| 135 | } |
| 136 | done(QDialog::Accepted); |
| 137 | } |
| 138 | |
| 139 | DragDropVersions::DragDropVersions(QString filename, bool d,QWidget *parent) :QWidget(parent){ |
| 140 | /*! |
| 141 | * Gets the directory of filename and |
| 142 | * loads in the subdirectory "PROGRAM_NAME"saves (usualy shelXlesaves/) the file SAVEHIST. |
| 143 | * In that file each entry is seperated by something like "Entry|@|2011-03-08T13:32:19|@|b11.res|@|" |
| 144 | * @param filename the file path of the res or ins file |
| 145 | * @param parent parent widget |
| 146 | */ |
| 147 | QString dn=filename.section('/',0,-2), |
| 148 | argument=filename.section('/',-1); |
| 149 | |
| 150 | QString insorres=argument; |
| 151 | if (insorres.endsWith("res",Qt::CaseInsensitive)) insorres.replace(QRegularExpression("res$", QRegularExpression::CaseInsensitiveOption),"ins"); |
| 152 | else if (insorres.endsWith("ins",Qt::CaseInsensitive)) insorres.replace(QRegularExpression("ins$", QRegularExpression::CaseInsensitiveOption),"res"); |
| 153 | dark=d; |
| 154 | current=-1; |
| 155 | QString saveHistName=QString("%1/%2saves/SAVEHIST").arg(dn).arg(PROGRAM_NAME); |
| 156 | QFile sahi(saveHistName); |
| 157 | sahi.open(QIODevice::ReadOnly|QIODevice::Text); |
| 158 | QString Alles=sahi.readAll();//Entry|@|2011-03-08T13:32:19|@|b11.res|@| |
| 159 | sahi.close(); |
| 160 | QRegularExpression re = QRegularExpression("Entry\\|@\\|\\w+-\\w+-\\w+:\\w+:\\w+\\|@\\|[^@]+@\\|"); |
| 161 | QRegularExpressionMatchIterator matchiterator = re.globalMatch(Alles); |
| 162 | entries = Alles.split(re,skipEmptyParts); |
| 163 | QStringList filenames; |
| 164 | while (matchiterator.hasNext()) { |
| 165 | QString recap0 = matchiterator.next().captured(); |
| 166 | zeiten.append(QDateTime::fromString(recap0.section('|',2,2),Qt::ISODate)); |
| 167 | filenames.append(recap0.section('|',4,4)); |
| 168 | //re.indexIn(Alles,pos+30); |
| 169 | //pos=re.pos(0); |
| 170 | } |
| 171 | now = QDateTime::currentDateTime(); |
| 172 | for (int j=0; j<filenames.size(); j++) |
| 173 | for (int i=0; i<filenames.size(); i++){ |
| 174 | if ((filenames.at(i)!=argument)&&(filenames.at(i)!=insorres)){ |
| 175 | entries.removeAt(i); |
| 176 | zeiten.removeAt(i); |
| 177 | filenames.removeAt(i); |
| 178 | } |
| 179 | } |
| 180 | int x = 5; |
| 181 | int y = 5; |
| 182 | int row=0,lawi=0,ymax=0; |
| 183 | DragVersion * l; |
| 184 | uint beast=666; |
| 185 | l =new DragVersion(-1,QString("current file version"),beast, -1, dark, this); |
| 186 | l->move(5, 5); |
| 187 | l->show(); |
| 188 | breite = lawi = itemwidth=l->width()+25; |
| 189 | hoehe = l->height()+25; |
| 190 | x=lawi+5; |
| 191 | for (int i=0; i<zeiten.size(); i++){ |
| 192 | DragVersion *wordLabel = new DragVersion(i,zeiten.at(i).toString(),zeiten.at(i).toSecsSinceEpoch(),-1,dark , this); |
| 193 | wordLabel->move(x, y); |
| 194 | wordLabel->show(); |
| 195 | wordLabel->setAttribute(Qt::WA_DeleteOnClose); |
| 196 | itemheight=wordLabel->height(); |
| 197 | itemwidth = qMax(wordLabel->width()+25,itemwidth); |
| 198 | y += wordLabel->height() + 2; |
| 199 | ymax=qMax(ymax,y+itemheight); |
| 200 | if (y / height()) { |
| 201 | lawi = itemwidth; |
| 202 | row++; |
| 203 | } |
| 204 | x = (y / height())? lawi+x-20 : x; |
| 205 | y = (y / height())? 5 : y; |
| 206 | }// */ |
| 207 | |
| 208 | QPalette newPalette = palette(); |
| 209 | newPalette.setColor(QPalette::Window, Qt::white); |
| 210 | setPalette(newPalette); |
| 211 | |
| 212 | setFixedSize(itemwidth+x+5, qMax(200, ymax)); |
| 213 | tick = new QTimer(this); |
| 214 | connect(tick,SIGNAL(timeout()), this , SLOT(tack())); |
| 215 | tick->start(500); |
| 216 | setAcceptDrops(true); |
| 217 | } |
| 218 | |
| 219 | void DragDropVersions::paintEvent(QPaintEvent *event){ |
| 220 | QPainter p(this); |
| 221 | p.fillRect(event->rect(), (dark)?QColor("#455364"):QColor("#eeeeee")); |
| 222 | p.fillRect(0, 0, breite, hoehe, QColor("#aac0cc")); |
| 223 | } |
| 224 | |
| 225 | void DragDropVersions::tack(){ |
| 226 | QObjectList kinder =children () ; |
| 227 | now = QDateTime::currentDateTime(); |
| 228 | for (int i=0; i<kinder.size(); i++){ |
| 229 | DragVersion *child = static_cast<DragVersion*>(kinder.at(i)); |
| 230 | if (!child||!child->inherits("QLabel")) continue; |
| 231 | int idx=child->index(); |
| 232 | if ((idx==-1)&&(child->secs()==666)) { |
| 233 | if (dark)child->setStyleSheet (QString( |
| 234 | "QLabel{color: #ffffff; font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #242424);} " |
| 235 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 236 | "stop: 0 #424242, stop: 1 %1);border: 0px solid #eeeeee;;}").arg((idx==current)?"#007799":"#007700")); |
| 237 | else child->setStyleSheet (QString( |
| 238 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #dadbde);} " |
| 239 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 240 | "stop: 0 #ffffff, stop: 1 %1);border: 2px solid #eeeeee;}").arg(QColor((idx==current)?"#aaeeaa":"#ffeecc").name())); |
| 241 | } |
| 242 | if ((idx<0)||(idx>=zeiten.size())||(child->secs()==666)) continue; |
| 243 | //child->setText(seitkurzem(now.secsTo(QDateTime::fromTime_t (child->secs())))); |
| 244 | child->setText(QDateTime::fromSecsSinceEpoch(child->secs()).toString()); |
| 245 | if (dark)child->setStyleSheet (QString( |
| 246 | "QLabel{color: #ffffff; font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #242424);} " |
| 247 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 248 | "stop: 0 #424242, stop: 1 %1);border: 0px solid #eeeeee;;}").arg((idx==current)?"#007799":"#007700")); |
| 249 | else child->setStyleSheet (QString( |
| 250 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #dadbde);} " |
| 251 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 252 | "stop: 0 #ffffff, stop: 1 %1);border: 2px solid #eeeeee;}").arg(QColor((idx==current)?"#aaeeaa":"#ffeecc").name())); |
| 253 | child->adjustSize (); |
| 254 | }// */ |
| 255 | } |
| 256 | |
| 257 | void DragDropVersions::mousePressEvent(QMouseEvent *event){ |
| 258 | DragVersion *child = static_cast<DragVersion*>(childAt(event->pos())); |
| 259 | if (!child) |
| 260 | return; |
| 261 | current = child->index(); |
| 262 | emit indexChanged(current); |
| 263 | } |
| 264 | |
| 265 | //////////////////////////////////////////////////////////////////////////////// |
| 266 | DragVersion::DragVersion(const int index,const QString &text, uint secs,const int cur, bool d, QWidget *parent): QLabel(parent){ |
| 267 | m_labelText = text; |
| 268 | m_index = index; |
| 269 | m_secs = secs; |
| 270 | dark=d; |
| 271 | setText(m_labelText); |
| 272 | if (dark){ |
| 273 | setStyleSheet (QString( |
| 274 | "QLabel{color: #ffffff; font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #242424);} " |
| 275 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 276 | "stop: 0 #424242, stop: 1 %1);border: 0px solid #eeeeee;;}").arg((index==cur)?"#007799":"#007700")); |
| 277 | }else{ |
| 278 | setStyleSheet (QString( |
| 279 | "QLabel{font: bold; border: 2px solid #2f2f31; border-radius: 1px;background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 %1, stop: 1 #dadbde);} " |
| 280 | "QLabel:hover {background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0," |
| 281 | "stop: 0 #ffffff, stop: 1 %1);border: 2px solid #eeeeee;}").arg(QColor((index==cur)?"#aaeeaa":"#ffeecc").name())); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | QString DragVersion::labelText() const{ |
| 286 | return m_labelText; |
| 287 | } |
| 288 | |
| 289 | int DragVersion::index() const{ |
| 290 | return m_index; |
| 291 | } |
| 292 | |
| 293 | uint DragVersion::secs() const{ |
| 294 | return m_secs; |
| 295 | } |
| 296 |