| 1 | /* |
| 2 | * File: TapeSplt.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 31, 2010 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "TapeSplt.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "UtilityRunner.h" |
| 28 | #include "Preferences.h" |
| 29 | #include <QLineEdit> |
| 30 | |
| 31 | #include <QFileDialog> |
| 32 | #include <QMessageBox> |
| 33 | |
| 34 | #include <csignal> |
| 35 | |
| 36 | TapeSplt::TapeSplt(QWidget *parent) |
| 37 | : GenericUtility("tapesplt",parent) |
| 38 | { |
| 39 | ui.setupUi(this); |
| 40 | ui.tableView->setEditTriggers(QTableView::NoEditTriggers); |
| 41 | connect(ui.runButton, SIGNAL(clicked()), this, SLOT(runClicked())); |
| 42 | connect(ui.exitButton, SIGNAL(clicked()), this, SLOT(cancelClicked())); |
| 43 | connect(ui.inputSelButton, SIGNAL(clicked()), this, SLOT(selectInputClicked())); |
| 44 | connect(ui.addButton, SIGNAL(clicked()), this, SLOT(addClicked())); |
| 45 | connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked())); |
| 46 | connect(ui.downButton, SIGNAL(clicked()), this, SLOT(downClicked())); |
| 47 | connect(ui.upButton, SIGNAL(clicked()), this, SLOT(upClicked())); |
| 48 | mMaxFile = ui.tableView->geometry().width()/8-8; |
| 49 | } |
| 50 | |
| 51 | TapeSplt::~TapeSplt() |
| 52 | { |
| 53 | |
| 54 | } |
| 55 | |
| 56 | void TapeSplt::runClicked() |
| 57 | { |
| 58 | if (!runOrStopClicked()) |
| 59 | { |
| 60 | ui.runButton->setText("Run"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (ui.inputFile->text().isEmpty()) |
| 65 | { |
| 66 | QMessageBox::warning(this, "tapesplt", "Please specify input file to process", |
| 67 | QMessageBox::Ok, QMessageBox::NoButton); |
| 68 | return; |
| 69 | } |
| 70 | if (!QFile::exists(ui.inputFile->text())) |
| 71 | { |
| 72 | QMessageBox::warning(this, "tapesplt", "The specified file does not exist", |
| 73 | QMessageBox::Ok, QMessageBox::NoButton); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | std::vector<std::string> parameters; |
| 78 | |
| 79 | parameters.push_back(ui.inputFile->text().toStdString()); |
| 80 | |
| 81 | for (int i=0; i<mModel->rowCount(); i++) |
| 82 | { |
| 83 | QModelIndex ix = mModel->index(i,0,QModelIndex()); |
| 84 | QString row = mModel->data(ix).toString(); |
| 85 | QString fileName = row.left(mMaxFile).trimmed(); |
| 86 | QString blocks = row.mid(mMaxFile); |
| 87 | ui.inputFile->setText(fileName); |
| 88 | parameters.push_back(fileName.toStdString()); |
| 89 | parameters.push_back(blocks.toStdString()); |
| 90 | } |
| 91 | |
| 92 | std::string command = "tapesplt"; |
| 93 | |
| 94 | execute(command, Preferences::getInstance().hercDir(), parameters); |
| 95 | ui.runButton->setText("Stop"); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void TapeSplt::cancelClicked() |
| 100 | { |
| 101 | deleteLater(); |
| 102 | } |
| 103 | |
| 104 | void TapeSplt::selectInputClicked() |
| 105 | { |
| 106 | QString s = QFileDialog::getOpenFileName(this,"Browse for input tape file",ui.inputFile->text()); |
| 107 | ui.inputFile->setText(s); |
| 108 | } |
| 109 | |
| 110 | void TapeSplt::addClicked() |
| 111 | { |
| 112 | TapeSpltSubDlg * subDlg = new TapeSpltSubDlg(this); |
| 113 | connect (subDlg, SIGNAL(fileNameSet(QString, int)), this, SLOT(fileNameSetSlot(QString, int))); |
| 114 | subDlg->exec(); |
| 115 | } |
| 116 | |
| 117 | void TapeSplt::deleteClicked() |
| 118 | { |
| 119 | int i = ui.tableView->currentIndex().row(); |
| 120 | if (i>= 0) mModel->removeRow(i); |
| 121 | QStandardItemModel t; |
| 122 | ui.tableView->setModel(&t); |
| 123 | ui.tableView->setModel(mModel); |
| 124 | ui.tableView->resizeColumnToContents(0); |
| 125 | } |
| 126 | |
| 127 | void TapeSplt::downClicked() |
| 128 | { |
| 129 | int i = ui.tableView->currentIndex().row(); |
| 130 | if (i>=0 && (i+1)<mModel->rowCount()) |
| 131 | { |
| 132 | QVariant itemA = mModel->rowData(ui.tableView->currentIndex()); |
| 133 | QVariant itemB = mModel->rowData(ui.tableView->currentIndex().sibling(i+1,0)); |
| 134 | mModel->setData(ui.tableView->currentIndex(),itemB); |
| 135 | mModel->setData(ui.tableView->currentIndex().sibling(i+1,0),itemA); |
| 136 | ui.tableView->setCurrentIndex(ui.tableView->currentIndex().sibling(i+1,0)); |
| 137 | QStandardItemModel t; |
| 138 | ui.tableView->setModel(&t); |
| 139 | ui.tableView->setModel(mModel); |
| 140 | ui.tableView->resizeColumnToContents(0); } |
| 141 | } |
| 142 | void TapeSplt::upClicked() |
| 143 | { |
| 144 | int i = ui.tableView->currentIndex().row(); |
| 145 | if (i>=1 && mModel->rowCount() > 1) |
| 146 | { |
| 147 | QVariant itemA = mModel->rowData(ui.tableView->currentIndex().sibling(i-1,0)); |
| 148 | QVariant itemB = mModel->rowData(ui.tableView->currentIndex()); |
| 149 | mModel->setData(ui.tableView->currentIndex(),itemA); |
| 150 | mModel->setData(ui.tableView->currentIndex().sibling(i-1,0),itemB); |
| 151 | ui.tableView->setCurrentIndex(ui.tableView->currentIndex().sibling(i-1,0)); |
| 152 | QStandardItemModel t; |
| 153 | ui.tableView->setModel(&t); |
| 154 | ui.tableView->setModel(mModel); |
| 155 | ui.tableView->resizeColumnToContents(0); } |
| 156 | } |
| 157 | void TapeSplt::fileNameSetSlot(QString fileName, int blocks) |
| 158 | { |
| 159 | if (fileName == "") return; |
| 160 | if (ui.tableView->model() == NULL) |
| 161 | { |
| 162 | mModel = new HTableModel(this); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | QStandardItemModel t; |
| 167 | ui.tableView->setModel(&t); |
| 168 | } |
| 169 | mModel->insertRow(mModel->rowCount()-1, QModelIndex(), fileName, QString::number(blocks)); |
| 170 | ui.tableView->setModel(mModel); |
| 171 | ui.tableView->resizeColumnToContents(0); |
| 172 | } |
| 173 | |
| 174 | QString TapeSplt::genLine(QString file, int blocks) |
| 175 | { |
| 176 | QString ret = file.leftJustified(mMaxFile,' ') + QString::number(blocks); |
| 177 | return file.leftJustified(mMaxFile,' ') + QString::number(blocks); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void TapeSplt::finishedSlot() |
| 182 | { |
| 183 | if (mFinishedOK) |
| 184 | { |
| 185 | QMessageBox::information(this, "tapsplt", "Tape split successfully completed!", |
| 186 | QMessageBox::Ok, |
| 187 | QMessageBox::NoButton); |
| 188 | deleteLater(); |
| 189 | } |
| 190 | else |
| 191 | emit error(); |
| 192 | |
| 193 | ui.runButton->setText("Run"); |
| 194 | } |
| 195 | |
| 196 | TapeSpltSubDlg::TapeSpltSubDlg(QWidget * parent) : QDialog(parent) |
| 197 | { |
| 198 | setupUi(); |
| 199 | connect(selOutputButton, SIGNAL(clicked()), this, SLOT(selectOutputSlot())); |
| 200 | connect(addButton, SIGNAL(clicked()), this, SLOT(addSlot())); |
| 201 | connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelSlot())); |
| 202 | } |
| 203 | |
| 204 | TapeSpltSubDlg::~TapeSpltSubDlg() |
| 205 | { |
| 206 | |
| 207 | } |
| 208 | |
| 209 | void TapeSpltSubDlg::setupUi() |
| 210 | { |
| 211 | resize(554, 204); |
| 212 | label = new QLabel(this); |
| 213 | label->setObjectName(QString::fromUtf8("label")); |
| 214 | label->setGeometry(QRect(30, 30, 461, 16)); |
| 215 | lineEdit = new QLineEdit(this); |
| 216 | lineEdit->setObjectName(QString::fromUtf8("lineEdit")); |
| 217 | lineEdit->setGeometry(QRect(30, 60, 461, 20)); |
| 218 | selOutputButton = new QPushButton(this); |
| 219 | selOutputButton->setObjectName(QString::fromUtf8("selOutputButton")); |
| 220 | selOutputButton->setGeometry(QRect(510, 60, 31, 23)); |
| 221 | spinBox = new QSpinBox(this); |
| 222 | spinBox->setObjectName(QString::fromUtf8("spinBox")); |
| 223 | spinBox->setGeometry(QRect(30, 120, 42, 22)); |
| 224 | spinBox->setMaximum(9); |
| 225 | spinBox->setValue(1); |
| 226 | label_2 = new QLabel(this); |
| 227 | label_2->setObjectName(QString::fromUtf8("label_2")); |
| 228 | label_2->setGeometry(QRect(30, 90, 251, 16)); |
| 229 | addButton = new QPushButton(this); |
| 230 | addButton->setObjectName(QString::fromUtf8("addButton")); |
| 231 | addButton->setGeometry(QRect(30, 160, 75, 23)); |
| 232 | cancelButton = new QPushButton(this); |
| 233 | cancelButton->setObjectName(QString::fromUtf8("cancelButton")); |
| 234 | cancelButton->setGeometry(QRect(460, 160, 75, 23)); |
| 235 | |
| 236 | |
| 237 | setWindowTitle("Add Output Tape"); |
| 238 | label->setText("Name of output file:"); |
| 239 | selOutputButton->setText("..."); |
| 240 | label_2->setText("Number of blocks to extarct into this tape file:"); |
| 241 | addButton->setText("Add"); |
| 242 | cancelButton->setText("Cancel"); |
| 243 | } |
| 244 | |
| 245 | void TapeSpltSubDlg::selectOutputSlot() |
| 246 | { |
| 247 | QString s = QFileDialog::getOpenFileName(this,"Browse for output tape file",lineEdit->text()); |
| 248 | lineEdit->setText(s); |
| 249 | } |
| 250 | |
| 251 | void TapeSpltSubDlg::addSlot() |
| 252 | { |
| 253 | emit fileNameSet(lineEdit->text(), spinBox->value()); |
| 254 | deleteLater(); |
| 255 | } |
| 256 | |
| 257 | void TapeSpltSubDlg::cancelSlot() |
| 258 | { |
| 259 | deleteLater(); |
| 260 | } |
| 261 | |
| 262 | ////////////////////////////////////// |
| 263 | |
| 264 | HTableModel::HTableModel(QObject *o) : QAbstractTableModel(o) |
| 265 | { |
| 266 | |
| 267 | } |
| 268 | |
| 269 | HTableModel::~HTableModel() |
| 270 | { |
| 271 | |
| 272 | } |
| 273 | |
| 274 | bool HTableModel::insertRow(int row, const QModelIndex &index, QString filename, QString blocks) |
| 275 | { |
| 276 | hOutDebug(3,"insert " << filename.toStdString() << " to row "<< row); |
| 277 | |
| 278 | if (row > static_cast<int>(mTable.size())) return false; |
| 279 | HTableType::iterator it = mTable.begin()+(1+row); |
| 280 | QStringList list; |
| 281 | list << filename << blocks ; |
| 282 | |
| 283 | beginInsertRows(index, row+1, row+2); |
| 284 | mTable.insert(it, list); |
| 285 | endInsertRows(); |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | bool HTableModel::removeRow ( int row, const QModelIndex & parent) |
| 291 | { |
| 292 | if (parent.isValid()) return false; |
| 293 | if (row >= static_cast<int>(mTable.size())) return false; |
| 294 | HTableType::iterator it = mTable.begin()+row; |
| 295 | mTable.erase(it); |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | int HTableModel::rowCount(const QModelIndex &parent) const |
| 300 | { |
| 301 | if (parent.isValid()) return 0; |
| 302 | else return mTable.size(); |
| 303 | } |
| 304 | |
| 305 | int HTableModel::columnCount(const QModelIndex &parent) const |
| 306 | { |
| 307 | if (parent.isValid()) return 0; |
| 308 | else return 2; |
| 309 | } |
| 310 | |
| 311 | QVariant HTableModel::data(const QModelIndex &index, int role) const |
| 312 | { |
| 313 | if (index.row() >= static_cast<int>(mTable.size())) return QVariant(); |
| 314 | QStringList list = mTable[index.row()]; |
| 315 | hOutDebug(5,"data(): request for col=" << index.column() << ";row=" << index.row() <<";role=" << role); |
| 316 | hOutDebug(5," list with size=" << list.size()); |
| 317 | if (role != Qt::DisplayRole) return QVariant(); |
| 318 | if (index.column() == 0) return QVariant(list[0]); |
| 319 | else return QVariant(list[1]); |
| 320 | } |
| 321 | |
| 322 | QVariant HTableModel::rowData(const QModelIndex &index) const |
| 323 | { |
| 324 | if (index.row() >= static_cast<int>(mTable.size())) return QVariant(); |
| 325 | QStringList list = mTable[index.row()]; |
| 326 | return QVariant(list); |
| 327 | } |
| 328 | |
| 329 | QVariant HTableModel::headerData ( int col, Qt::Orientation orientation, int role ) const |
| 330 | { |
| 331 | if (orientation == Qt::Horizontal && role == 0) |
| 332 | { |
| 333 | if (col == 0) return QVariant(QString("File-Name")); |
| 334 | else return QVariant(QString("Blocks")); |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | return QVariant(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | bool HTableModel::setData(const QModelIndex &index, const QVariant &value, int) |
| 343 | { |
| 344 | if (static_cast<int>(mTable.size()) <= index.row()) return false; |
| 345 | if (!value.isValid() || !value.canConvert(QVariant::StringList)) return false; |
| 346 | |
| 347 | QStringList list = value.toStringList(); |
| 348 | mTable[index.row()] = list; |
| 349 | return true; |
| 350 | } |