1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* This file is part of the xTuple ERP: PostBooks Edition, a free and
* open source Enterprise Resource Planning software suite,
* Copyright (c) 1999-2012 by OpenMFG LLC, d/b/a xTuple.
* It is licensed to you under the Common Public Attribution License
* version 1.0, the full text of which (including xTuple-specific Exhibits)
* is available at www.xtuple.com/CPAL. By using this software, you agree
* to be bound by its terms.
*/
#include <QtGui>
#include "csvimpplugin.h"
#include <QMainWindow>
#include "batchmessagehandler.h"
#include "csvatlaswindow.h"
#include "csvtoolwindow.h"
#include "interactivemessagehandler.h"
#define DEBUG false
CSVImpPlugin::CSVImpPlugin(QObject *parent)
: QObject(parent)
{
_atlasdir = QString::null;
_atlaswindow = 0;
_csvdir = QString::null;
_csvtoolwindow = 0;
_msghandler = 0;
}
QMainWindow *CSVImpPlugin::getCSVAtlasWindow(QWidget *parent, Qt::WindowFlags flags)
{
if (! _atlaswindow)
{
CSVToolWindow *csvtool = qobject_cast<CSVToolWindow*>(getCSVToolWindow(parent, flags));
if (csvtool)
{
_atlaswindow = csvtool->atlasWindow();
if (_msghandler)
_atlaswindow->setMessageHandler(_msghandler);
connect(_atlaswindow, SIGNAL(destroyed(QObject*)), this, SLOT(cleanupDestroyedObject(QObject*)));
if (_atlasdir.isEmpty())
_atlaswindow->setDir(_csvdir);
else
_atlaswindow->setDir(_atlasdir);
}
}
if (DEBUG)
qDebug("CSVImpPlugin::getAtlasToolWindow() returning %p, Atlas dir [%s]",
_atlaswindow, qPrintable(_atlasdir));
return _atlaswindow;
}
QMainWindow *CSVImpPlugin::getCSVToolWindow(QWidget *parent, Qt::WindowFlags flags)
{
if (! _csvtoolwindow)
{
_csvtoolwindow = new CSVToolWindow(parent, flags);
connect(_csvtoolwindow, SIGNAL(destroyed(QObject*)), this, SLOT(cleanupDestroyedObject(QObject*)));
_csvtoolwindow->sFirstRowHeader(_firstLineIsHeader);
_csvtoolwindow->setDir(_csvdir);
if (_atlasdir.isEmpty())
_csvtoolwindow->atlasWindow()->setDir(_csvdir);
else
_csvtoolwindow->atlasWindow()->setDir(_atlasdir);
if (_msghandler)
_csvtoolwindow->setMessageHandler(_msghandler);
}
if (DEBUG)
qDebug("CSVImpPlugin::getCSVToolWindow() returning %p "
"with CSV dir %s, Atlas dir %s",
_csvtoolwindow, qPrintable(_csvdir), qPrintable(_atlasdir));
return _csvtoolwindow;
}
bool CSVImpPlugin::importCSV()
{
_csvtoolwindow->clearImportLog();
return _csvtoolwindow->importStart();
}
bool CSVImpPlugin::isInteractive()
{
if (_msghandler && qobject_cast<BatchMessageHandler*>(_msghandler))
return false;
return true; // assume true since that's the tool window's default
}
QString CSVImpPlugin::lastError()
{
QString msg = QString::null;
if (_msghandler)
{
QtMsgType type = QtCriticalMsg;
QStringList msgs = _msghandler->unhandledMessages(&type);
if (! msgs.isEmpty())
msg = msgs.last();
}
return msg;
}
bool CSVImpPlugin::openAtlas(QString filename)
{
if (DEBUG) qDebug("CSVImpPlugin::openAtlas(%s)", qPrintable(filename));
CSVAtlasWindow *atlaswind = qobject_cast<CSVAtlasWindow*>(getCSVAtlasWindow(qobject_cast<QWidget*>(parent())));
if (atlaswind)
{
atlaswind->fileOpen(filename);
if (DEBUG)
qDebug("CSVImpPlugin::openAtlas() opened [%s]", qPrintable(filename));
return true;
}
return false;
}
bool CSVImpPlugin::openCSV(QString filename)
{
CSVToolWindow *csvtool = qobject_cast<CSVToolWindow*>(getCSVToolWindow(qobject_cast<QWidget*>(parent())));
if (csvtool)
{
csvtool->fileOpen(filename);
return true;
}
return false;
}
void CSVImpPlugin::setAtlasDir(QString dirname)
{
if (DEBUG) qDebug("CSVImpPlugin::setAltasDir(%s)", qPrintable(dirname));
_atlasdir = dirname;
if (_csvtoolwindow)
_csvtoolwindow->atlasWindow()->setDir(dirname);
}
bool CSVImpPlugin::setAtlasMap(const QString mapname)
{
if (_csvtoolwindow && _csvtoolwindow->atlasWindow())
return _csvtoolwindow->atlasWindow()->setMap(mapname);
return false;
}
void CSVImpPlugin::setCSVDir(QString dirname)
{
if (DEBUG) qDebug("CSVImpPlugin::setCSVDir(%s)", qPrintable(dirname));
_csvdir = dirname;
if (_csvtoolwindow)
_csvtoolwindow->setDir(dirname);
}
bool CSVImpPlugin::setFirstLineHeader(bool isheader)
{
if (DEBUG) qDebug("CSVImpPlugin::setFirstLineHeader(%d)", isheader);
_firstLineIsHeader = isheader;
if (_csvtoolwindow)
_csvtoolwindow->sFirstRowHeader(isheader);
return true;
}
void CSVImpPlugin::setInteractive(bool interactive)
{
if (isInteractive() != interactive)
{
if (_msghandler)
delete _msghandler;
if (interactive)
_msghandler = new InteractiveMessageHandler(parent());
else
_msghandler = new BatchMessageHandler(parent());
}
if (_msghandler)
{
if (_csvtoolwindow)
_csvtoolwindow->setMessageHandler(_msghandler);
if (_atlaswindow)
_atlaswindow->setMessageHandler(_msghandler);
}
}
void CSVImpPlugin::cleanupDestroyedObject(QObject *object)
{
if (DEBUG)
qDebug("CSVImpPlugin::cleanupDestroyedObject(%s %p)",
object ? object->metaObject()->className() : "[unknown]", object);
if (object == _csvtoolwindow)
{
_csvtoolwindow = 0;
_firstLineIsHeader = false;
}
else if (object == _atlaswindow)
_atlaswindow = 0;
else if (object == _msghandler)
_msghandler = 0;
}
Q_EXPORT_PLUGIN2(csvimpplugin, CSVImpPlugin);
|