| 1 | /* |
| 2 | * File: DevicesWidget.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 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 "DevicesWidget.h" |
| 26 | #include "Configuration.h" |
| 27 | #include "DeviceConfigLine.h" |
| 28 | #include "GenericDeviceProperties.h" |
| 29 | #include "Environment.h" |
| 30 | |
| 31 | #include <QMouseEvent> |
| 32 | #include <QMessageBox> |
| 33 | #include <QHeaderView> |
| 34 | |
| 35 | #include <iostream> |
| 36 | #include <sstream> |
| 37 | |
| 38 | DevicesWidget::DevicesWidget(ConfigFile * configFile, QWidget * parent) : |
| 39 | DeviceMenuProcessor(parent) , mConfigFile(configFile), mConfigView() |
| 40 | { |
| 41 | mConfigView = new DeviceConfigView(this); |
| 42 | mConfigView->setAutoScroll(false); |
| 43 | mConfigView->setGeometry(0,0, 680,420); |
| 44 | mConfigView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); |
| 45 | |
| 46 | QString iconPath = Environment::getIconsPath().c_str(); |
| 47 | mCardIcon = new QIcon(iconPath + "/card.jpg"); |
| 48 | mConsoleIcon = new QIcon(iconPath + "/card.jpg"); |
| 49 | mDasdIcon = new QIcon(iconPath + "/dasd.jpg"); |
| 50 | mNetIcon = new QIcon(iconPath + "/net.jpg"); |
| 51 | mPrinterIcon = new QIcon(iconPath + "/printer.jpg"); |
| 52 | mTapeIcon = new QIcon(iconPath + "/tape.jpg"); |
| 53 | mTerminalIcon = new QIcon(iconPath + "/terminal.jpg"); |
| 54 | mUnknownIcon = new QIcon(iconPath + "/unknown.jpg"); |
| 55 | |
| 56 | initialize(); |
| 57 | QPushButton * okButton = new QPushButton(this); |
| 58 | okButton->setText("OK"); |
| 59 | okButton->move(10,630); |
| 60 | |
| 61 | QPushButton * cancelButton = new QPushButton(this); |
| 62 | cancelButton->setText("Cancel"); |
| 63 | cancelButton->move(710,630); |
| 64 | |
| 65 | mConfigView->setVisible(true); |
| 66 | connect(mConfigView, SIGNAL(pressed(QModelIndex)),this,SLOT(mousePressed(QModelIndex))); |
| 67 | connect(mConfigView, SIGNAL(deviceClick(QMouseEvent *)),this,SLOT(devicesClick(QMouseEvent *))); |
| 68 | } |
| 69 | |
| 70 | DevicesWidget::~DevicesWidget() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void DevicesWidget::update(ConfigFile *configFile) |
| 75 | { |
| 76 | mConfigFile = configFile; |
| 77 | initialize(); |
| 78 | } |
| 79 | |
| 80 | void DevicesWidget::mousePressed(const QModelIndex index) |
| 81 | { |
| 82 | hOutDebug(3, "widget mouse pressed " << index.row()) |
| 83 | mClickRow = index.row(); |
| 84 | } |
| 85 | |
| 86 | bool DevicesWidget::realDevice() |
| 87 | { |
| 88 | return (mClickRow >=0 && isRealDev(mClickRow)); |
| 89 | } |
| 90 | |
| 91 | bool DevicesWidget::canAddSYSG() |
| 92 | { |
| 93 | return (mConfigFile->findFirstDeviceByType("SYSG") == NULL); |
| 94 | } |
| 95 | |
| 96 | DeviceTypes::Type DevicesWidget::getType(int lineNumber) |
| 97 | { |
| 98 | hOutDebug(3, "getType " << lineNumber << " " << mLastClick << std::endl); |
| 99 | DeviceConfigLine * line = mConfigFile->getDevice(mDialogToFile[lineNumber]); |
| 100 | return line->getDeviceType(); |
| 101 | } |
| 102 | |
| 103 | void DevicesWidget::menuDelete() |
| 104 | { |
| 105 | hOutDebug(5, "delete " << mLastClick); |
| 106 | mConfigFile->deleteLine(mDialogToFile[mLastClick]); |
| 107 | initialize(); |
| 108 | } |
| 109 | |
| 110 | void DevicesWidget::menuProperties() |
| 111 | { |
| 112 | hOutDebug(3, "properties " << mLastClick); |
| 113 | mCandidateLine = mConfigFile->getDevice(mDialogToFile[mLastClick]); |
| 114 | mCandidateLine->setNew(false); |
| 115 | GenericDeviceProperties * mProp = NULL; |
| 116 | |
| 117 | try |
| 118 | { |
| 119 | mProp = GenericDeviceProperties::classFactory(*mCandidateLine, this); |
| 120 | } |
| 121 | catch (hsException e) |
| 122 | { |
| 123 | std::string msg = e.what(); |
| 124 | QMessageBox::critical(this, "Invalid Device Line", msg.c_str() , QMessageBox::Ok); |
| 125 | } |
| 126 | catch (...) |
| 127 | { |
| 128 | hOutDebug(2, "exception caught in class factory (properties) "); |
| 129 | } |
| 130 | |
| 131 | if (mProp != NULL) |
| 132 | { |
| 133 | connect(mProp, SIGNAL(updateLine(bool)), this, SLOT(updateDevice(bool))); |
| 134 | mProp->setVisible(true); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void DevicesWidget::doAddDevice(bool keep) |
| 139 | { |
| 140 | hOutDebug(5,"Update Line "); |
| 141 | |
| 142 | std::stringstream newLine; |
| 143 | newLine << ConfigurationEditor::normalizeNum(mCandidateLine->getToken(0),4,16); |
| 144 | newLine << " " << mCandidateLine->getMultiToken(1,0) << "\n"; |
| 145 | mCandidateLine->replaceLine(newLine.str()); |
| 146 | |
| 147 | if (mCandidateLine != NULL) |
| 148 | { |
| 149 | if (keep && mCandidateLine->isNew()) |
| 150 | mConfigFile->addDev(mCandidateLine); |
| 151 | mCandidateLine = NULL; |
| 152 | } |
| 153 | initialize(); |
| 154 | } |
| 155 | |
| 156 | bool DevicesWidget::isUnique(std::string devno) |
| 157 | { |
| 158 | return (mConfigFile->getDevice(devno) == NULL); |
| 159 | } |
| 160 | |
| 161 | void DevicesWidget::initialize() |
| 162 | { |
| 163 | int lastSys = mConfigFile->getLastSys(); |
| 164 | QStandardItemModel * model = new QStandardItemModel(0,1); |
| 165 | model->clear(); |
| 166 | QStringList labels = (QStringList() << "Device"); |
| 167 | model->setHorizontalHeaderLabels(labels); |
| 168 | QStandardItem *parentItem = model->invisibleRootItem(); |
| 169 | mDialogToFile.clear(); |
| 170 | |
| 171 | hOutDebug(5, "DeviceDlg::initilize from " <<lastSys+1 << " to " << mConfigFile->size()); |
| 172 | int lineNum=-1; |
| 173 | for (int i=lastSys+1; i<mConfigFile->size(); i++) |
| 174 | { |
| 175 | const DeviceConfigLine * line = mConfigFile->getDevice(i); |
| 176 | hOutDebug(5,"line=" << line->getLine() << line->isRemark()); |
| 177 | if (line->isRemark()) continue; |
| 178 | |
| 179 | std::string strLine = line->getToken(0); |
| 180 | strLine.resize(10,' '); |
| 181 | strLine += line->getToken(1).c_str(); |
| 182 | strLine.resize(20,' '); |
| 183 | strLine += line->getMultiToken(2,0).c_str(); |
| 184 | strLine.resize(133,' '); |
| 185 | QStandardItem * item = new QStandardItem(strLine.c_str()); |
| 186 | item->setIcon(DeviceTypes::instance().getIcon(line->getDeviceType())); |
| 187 | |
| 188 | parentItem->appendRow(item); |
| 189 | ++lineNum; |
| 190 | mDialogToFile[lineNum] = i; |
| 191 | } |
| 192 | mConfigView->setModel(model); |
| 193 | mConfigView->setColumnWidth(0,800); |
| 194 | QFont font("Mono", 10); |
| 195 | mConfigView->setFont(font); |
| 196 | for (int i=0; i<model->rowCount(); i++) |
| 197 | mConfigView->setRowHeight(i,18); |
| 198 | mConfigView->setGridStyle(Qt::NoPen); |
| 199 | mConfigView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 200 | } |
| 201 | |
| 202 | void DevicesWidget::updateDevice(bool done) |
| 203 | { |
| 204 | if (!done || mCandidateLine == NULL) |
| 205 | { |
| 206 | mClickRow = -1; |
| 207 | return; |
| 208 | } |
| 209 | initialize(); |
| 210 | mClickRow = -1; |
| 211 | } |
| 212 |