Added: creating new bnp files

Fixed: some bugs caused crashes during drag&drop
This commit is contained in:
Krolock 2012-03-04 19:09:41 +01:00
parent 41551e3026
commit 062c3a84d5
10 changed files with 205 additions and 31 deletions

View file

@ -40,11 +40,12 @@ CBnpDirTreeDialog::CBnpDirTreeDialog(QString bnpPath, QWidget *parent)
// Bnp file: opened and displayed
// all other files: added to the currently opened bnp file
QStringList filter;
//filter << tr("*.bnp");
filter << tr("*.bnp");
// Setup the directory tree model
m_dirModel= new BNPFileSystemModel();
m_proxyModel = new BNPSortProxyModel();
m_ui.dirTree->setSortingEnabled(true);
m_dirModel->setRootPath(m_DataPath);
m_dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::AllEntries);
m_dirModel->setNameFilters(filter);
@ -55,7 +56,6 @@ CBnpDirTreeDialog::CBnpDirTreeDialog(QString bnpPath, QWidget *parent)
m_ui.dirTree->setModel(m_proxyModel);
m_ui.dirTree->setRootIndex( m_proxyModel->mapFromSource (m_dirModel->index(m_DataPath) ) );
m_ui.dirTree->setSortingEnabled(true);
// Trigger if one filename is activated
// In future drag&drop should be also possible

View file

@ -44,6 +44,9 @@
<height>0</height>
</size>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>

View file

@ -62,6 +62,15 @@ void BNPFileHandle::releaseInstance()
}
}
// ***************************************************************************
void BNPFileHandle::createFile(string filePath)
{
// Only set the filepath. Header will be created after files have been added
m_openedBNPFile = filePath;
m_packedFiles.clear();
nlinfo("Created file %s.", filePath.c_str() );
}
// ***************************************************************************
bool BNPFileHandle::unpack(const string &dirName, const vector<string>& fileList)
{
CIFile bnp;
@ -232,6 +241,8 @@ void BNPFileHandle::addFiles( const vector<string> &filePathes)
writeHeader(m_openedBNPFile + ".tmp", OffsetFromBegining);
// Delete any previous existing file
if (CFile::fileExists( m_openedBNPFile ))
CFile::deleteFile( m_openedBNPFile );
string src = m_openedBNPFile + ".tmp";
CFile::moveFile( m_openedBNPFile.c_str(), src.c_str() );

View file

@ -88,6 +88,12 @@ public:
*/
void fileNames( std::vector<std::string>& fileNames );
/**
* Create a new bnp file
* \param string file path
*/
void createFile( std::string filePath );
/**
* Add files to the current aktive bnp file
* \param vector of file pathes to add

View file

@ -20,10 +20,6 @@
// Qt includes
#include <QtGui/QWidget>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QUrl>
#include <QEvent>
// NeL includes
#include <nel/misc/debug.h>
@ -38,7 +34,6 @@ BnpFileListDialog::BnpFileListDialog(QString bnpPath, QWidget *parent)
m_DataPath(bnpPath)
{
m_ui.setupUi(this);
setAcceptDrops(true);
}
// ***************************************************************************
BnpFileListDialog::~BnpFileListDialog()
@ -111,6 +106,14 @@ bool BnpFileListDialog::loadTable(const QString filePath)
return true;
}
// ***************************************************************************
void BnpFileListDialog::clearTable()
{
// create emtpy table
setupTable(0);
setWindowTitle("BNP File List");
}
// ***************************************************************************
void BnpFileListDialog::getSelections(TSelectionList& SelectionList)
{
QModelIndex index;
@ -125,21 +128,5 @@ void BnpFileListDialog::getSelections(TSelectionList& SelectionList)
SelectionList.push_back( filename.toStdString() );
}
}
// ***************************************************************************
void BnpFileListDialog::dragEnterEvent(QDragEnterEvent *event)
{
// Accept only one file
// In the future a tabbed FileListDialog would accept more
if ( event->mimeData()->hasUrls() && event->mimeData()->urls().count() == 1)
event->acceptProposedAction();
}
// ***************************************************************************
void BnpFileListDialog::dropEvent(QDropEvent *event)
{
// Excraft the local file url from the drop object and fill the table
const QMimeData *mimeData = event->mimeData();
QList<QUrl> urlList = mimeData->urls();
loadTable( urlList.first().toLocalFile() );
}
} // namespace BNPManager

View file

@ -59,6 +59,11 @@ public:
*/
void setupTable(int nbrows);
/**
* When BNP files is closed, clear the filelist table
*/
void clearTable();
/**
* Fill the files selected in the table view to
* unpack them.
@ -67,9 +72,6 @@ public:
*/
void getSelections(TSelectionList& SelectionList);
protected:
void dragEnterEvent (QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private:
Ui::BnpFileListDialog m_ui;

View file

@ -25,6 +25,7 @@ namespace Constants
const char * const BNP_MANAGER_SECTION = "BNPManager";
//resources
const char *const ICON_NEW = ":/images/ic_nel_new.png";
const char *const ICON_ADD = ":/images/ic_nel_add_item.png";
const char *const ICON_DELETE = ":/images/ic_nel_delete_item.png";
const char *const ICON_UNPACK = ":/images/ic_nel_export.png";

View file

@ -30,6 +30,10 @@
#include <nel/misc/debug.h>
#include <nel/misc/path.h>
// STL includes
#include <vector>
#include <string>
// Qt includes
#include <QDebug>
#include <QFileDialog>
@ -37,6 +41,11 @@
#include <QTableWidget>
#include <QMessageBox>
#include <QSettings>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QUrl>
#include <QEvent>
#include <QInputDialog>
using namespace std;
using namespace NLMISC;
@ -53,6 +62,8 @@ BNPManagerWindow::BNPManagerWindow(QWidget *parent)
setCentralWidget(hideWidget);
hideWidget->hide();
setAcceptDrops(true);
// Read the settings
readSettings();
@ -98,6 +109,12 @@ void BNPManagerWindow::createDialogs()
// ***************************************************************************
void BNPManagerWindow::createActions()
{
// new action
m_newAction = new QAction(tr("&New..."), this);
m_newAction->setIcon(QIcon(Core::Constants::ICON_NEW));
m_newAction->setStatusTip(tr("New file"));
connect(m_newAction, SIGNAL(triggered()), this, SLOT( newFile() ));
// open action
m_openAction = new QAction(tr("&Open..."), this);
m_openAction->setIcon(QIcon(Core::Constants::ICON_OPEN));
@ -132,6 +149,7 @@ void BNPManagerWindow::createActions()
void BNPManagerWindow::createToolBars()
{
m_fileToolBar = addToolBar(tr("&File"));
m_fileToolBar->addAction(m_newAction);
m_fileToolBar->addAction(m_openAction);
m_fileToolBar->addAction(m_closeAction);
@ -143,10 +161,36 @@ void BNPManagerWindow::createToolBars()
// ***************************************************************************
bool BNPManagerWindow::loadFile(const QString fileName)
{
// Store the filename for later use
m_openedBNPFile = fileName;
m_BnpFileListDialog->loadTable(fileName);
return true;
}
// ***************************************************************************
void BNPManagerWindow::newFile()
{
// reference to the BNPFileHandle singletone instance
BNPFileHandle& myBNPFileHandle = BNPFileHandle::getInstance();
m_openedBNPFile = "";
m_BnpFileListDialog->clearTable();
QString filePath = QFileDialog::getSaveFileName(this, tr("Create File"),QDir::currentPath(),
tr("BNP File (*.bnp)"));
if (filePath.isEmpty() )
return;
if ( !filePath.endsWith(".bnp", Qt::CaseInsensitive) )
filePath.append(".bnp");
m_openedBNPFile = filePath;
m_BnpFileListDialog->setWindowTitle (filePath);
myBNPFileHandle.createFile ( filePath.toStdString() );
}
// ***************************************************************************
void BNPManagerWindow::open()
{
QString fileName;
@ -158,13 +202,13 @@ void BNPManagerWindow::open()
if (fileName.isNull())
return;
m_openedBNPFile = fileName;
loadFile(fileName);
}
// ***************************************************************************
void BNPManagerWindow::close()
{
//TODO
m_openedBNPFile = "";
m_BnpFileListDialog->clearTable();
}
// ***************************************************************************
void BNPManagerWindow::addFiles()
@ -216,6 +260,49 @@ void BNPManagerWindow::addFiles()
loadFile(m_openedBNPFile);
}
// ***************************************************************************
void BNPManagerWindow::addFiles( QStringList FileList )
{
// reference to the BNPFileHandle singletone instance
BNPFileHandle& myBNPFileHandle = BNPFileHandle::getInstance();
// vector of all current packed filenames
vector<string> currentFiles;
// vector of files to add
vector<string> addFiles;
// get all current filenames from the opened bnp file
myBNPFileHandle.fileNames(currentFiles);
QStringList::iterator it_list = FileList.begin();
while (it_list != FileList.end() )
{
string fileName = CFile::getFilename (it_list->toStdString() );
if ( std::find(currentFiles.begin(), currentFiles.end(), fileName ) != currentFiles.end() )
{
// Ask the user if he wants to override the existing file
// atm only warn the user and do not override
QMessageBox::warning(this, tr("BNP Manager"),
tr("File is already in the list!"),
QMessageBox::Ok,
QMessageBox::Ok);
}
else
{
addFiles.push_back( it_list->toStdString() );
// log it
nlinfo("Add file %s", fileName.c_str() );
}
it_list++;
}
if ( !addFiles.empty() )
{
myBNPFileHandle.addFiles( addFiles );
}
loadFile(m_openedBNPFile);
}
// ***************************************************************************
void BNPManagerWindow::deleteFiles()
{
QFileDialog filedialog(this);
@ -288,4 +375,70 @@ void BNPManagerWindow::readSettings()
void BNPManagerWindow::writeSettings()
{
}
// ***************************************************************************
void BNPManagerWindow::dragEnterEvent(QDragEnterEvent *event)
{
// Accept only one file
// In the future a tabbed FileListDialog would accept more
if ( event->mimeData()->hasUrls() )
event->acceptProposedAction();
}
// ***************************************************************************
void BNPManagerWindow::dropEvent(QDropEvent *event)
{
// reference to the BNPFileHandle singletone instance
BNPFileHandle& myBNPFileHandle = BNPFileHandle::getInstance();
// Excraft the local file url from the drop object and fill the table
const QMimeData *mimeData = event->mimeData();
QList<QUrl> urlList = mimeData->urls();
QString filePath;
QStringList fileList;
if ( urlList.count() == 1 )
{
// If it is a bnp file, open it
// If it is not a bnp file add it
filePath = urlList.first().toLocalFile();
if ( filePath.endsWith(".bnp", Qt::CaseInsensitive) )
{
loadFile(filePath);
}
else
{
if ( m_openedBNPFile == "")
newFile();
// Create a QStringList and pass it to addfiles
fileList.push_back( filePath );
addFiles( fileList );
// Reload current bnp
loadFile(m_openedBNPFile);
}
}
else if ( urlList.count() > 1 )
{
// Dont accept any bnp file
QList<QUrl>::iterator it = urlList.begin();
while ( it != urlList.end() )
{
filePath = it->toLocalFile();
if ( filePath.endsWith(".bnp") )
{
nlwarning("Could not add a bnp file!", filePath.toStdString().c_str() );
}
else
{
fileList.push_back( filePath );
}
++it;
}
if ( m_openedBNPFile == "")
newFile();
addFiles( fileList );
// Reload current bnp
loadFile(m_openedBNPFile);
}
}
} // namespace BNPManager

View file

@ -59,9 +59,14 @@ public:
public Q_SLOTS:
/**
* Open a file dialog to choose which file should be opened.
* Create a new file
* \return Filename string
*/
void newFile();
/**
* Open a file dialog to choose which file should be opened.
*/
void open();
/**
@ -81,6 +86,7 @@ public Q_SLOTS:
* \param Filelist
*/
void addFiles();
void addFiles( QStringList FileList );
/**
* Unpack the files marked in the filelist dialog into user defined
@ -96,6 +102,10 @@ public Q_SLOTS:
*/
void deleteFiles();
protected:
void dragEnterEvent (QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
private:
/**
@ -126,6 +136,7 @@ private:
QToolBar *m_fileToolBar;
QToolBar *m_toolsBar;
QAction *m_newAction;
QAction *m_openAction;
QAction *m_closeAction;
QAction *m_addFilesAction;

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB