External drag drop

This commit is contained in:
Ian Gebbie 2020-02-08 18:51:52 +02:00
parent b02e9f8cdf
commit e773148a2b
10 changed files with 171 additions and 34 deletions

View file

@ -314,7 +314,7 @@ void AbstractData::parseData() {
}
void AbstractData::parseData(QDataStream& in) {
qDebug()<<"AbstractData::parseData(DS)";
qDebug()<<"AbstractData::parseData(DataStream)";
in.setByteOrder(QDataStream::LittleEndian);
qint8 c;
@ -322,7 +322,6 @@ void AbstractData::parseData(QDataStream& in) {
uint length;
length=16;
qDebug()<<"Parsing Matrix";
in.startTransaction();
while(!in.atEnd()) {
in>>c;
@ -905,7 +904,7 @@ void Program::setProg(QString data_in) {
void Program::parseData() {
QTextCodec * codec = QTextCodec::codecForName("UTF8");
QTextCodec * codec = QTextCodec::codecForName("ISO 8859-1");
QByteArray a1;
a1=getData();
text = codec->toUnicode(a1);
@ -915,7 +914,7 @@ void Program::parseData() {
void Program::parseData(QDataStream& in) {
QTextCodec * codec = QTextCodec::codecForName("UTF-16");
QTextCodec * codec = QTextCodec::codecForName("ISO 8859-1");
QByteArrayMatcher matcher;
QByteArray search;
QByteArray phrase;
@ -1008,7 +1007,7 @@ void Notes::parseData() {
// quint16 len1,len2;
int formatstart;
QTextCodec * codec = QTextCodec::codecForName("UTF-16LE");
QTextCodec * codec = QTextCodec::codecForName("UTF-8");
QByteArray a1,a3;

View file

@ -18,6 +18,7 @@
#include <global.h>
#include <cntfilesystemmodel.h>
#include <QMimeData>
#include <QList>
#include <QStringListModel>
#include <hp_mditexteditor.h>
@ -83,6 +84,8 @@ QMimeData* contentFileSystemModel::mimeData(const QModelIndexList &indexes) cons
qDebug()<<"contentFileSystemModel::mimeData";
foreach(index,indexes) {
index=indexes.first();
// qDebug()<<index.data(Qt::DisplayRole);
// QString data;
@ -108,6 +111,7 @@ QMimeData* contentFileSystemModel::mimeData(const QModelIndexList &indexes) cons
}
mimeDataPtr->setText(info.baseName());
switch (filedata.type) {
case HP_PROG:
@ -130,6 +134,20 @@ QMimeData* contentFileSystemModel::mimeData(const QModelIndexList &indexes) cons
}
file.close();
}
//Allow copy to external file managers
QList<QUrl> urls;
QFileInfo info;
QUrl url;
foreach(index,indexes) {
info = contentFileSystemModel::fileInfo(index);
url = QUrl::fromLocalFile(info.absoluteFilePath());
urls.append(url);
}
mimeDataPtr->setUrls(urls);
return mimeDataPtr;
}
@ -154,7 +172,18 @@ Qt::ItemFlags contentFileSystemModel::flags(const QModelIndex &index) const
//Allow drop in location
bool contentFileSystemModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
{
return true;
qDebug()<<data->formats();
if (data->hasUrls()) {
return true;
}
if (data->hasText()) {
return true;
}
return false;
}
//Process the drop action
@ -184,6 +213,29 @@ bool contentFileSystemModel::dropMimeData(const QMimeData* md_data, Qt::DropActi
return true;
}
//Check for external file drop
if (md_data->hasUrls()) {
QString name = md_data->text();
QList<QUrl> urls = md_data->urls();
QUrl url;
QString filefrom;
QString fileto;
foreach(url,urls) {
filefrom=url.toLocalFile();
QFileInfo fileinfo(filefrom);
fileto=path+"/"+url.fileName();
if(isFileType(fileinfo)) {
//consider check to limit unusual file types
QFile::copy(filefrom,fileto);
}
}
return true;
}
//rework
QByteArray data_in;
QByteArray typeary;
QString name = md_data->text();
@ -198,7 +250,6 @@ bool contentFileSystemModel::dropMimeData(const QMimeData* md_data, Qt::DropActi
qDebug()<<data_in;
qDebug()<<fileinfo;
// QDataStream stream(&encodedData, QIODevice::ReadOnly);
if (writeFile(fileinfo,data_in)>-1)
@ -305,6 +356,8 @@ AbstractData * contentFileSystemModel::readFile(QFileInfo fileinfo) const {
if (file.open(QIODevice::ReadOnly),QFileDevice::AutoCloseHandle) {
QDataStream in(&file);
// QTextCodec *codec = QTextCodec::codecForName("UTF-16");
// in.setCodec(codec);
in.setByteOrder(QDataStream::LittleEndian);
filedata=getFileType(fileinfo);
@ -317,6 +370,12 @@ AbstractData * contentFileSystemModel::readFile(QFileInfo fileinfo) const {
data->parseData(in);
}
break;
case HP_NOTE: {
qDebug()<<"HP_NOTE";
data = new Notes(filedata.filename, HP_NOTE, QStringLiteral(""));
data->parseData(in);
}
break;
default: ;
}
@ -332,10 +391,10 @@ int contentFileSystemModel::writeFile(QFileInfo fileinfo, QByteArray data_in) co
QFile file(fileinfo.absoluteFilePath());
if (file.open(QIODevice::ReadWrite)) {
QTextStream out(&file);
QTextCodec *codec = QTextCodec::codecForName("UTF-16");
out.setCodec(codec);
// out.setByteOrder(QDataStream::LittleEndian);
QDataStream out(&file);
// QTextCodec *codec = QTextCodec::codecForName("UTF-8");
// out.setCodec(codec);
out.setByteOrder(QDataStream::LittleEndian);
// quint8 c;
// for (int i =0 ;i< data_in.length();i++)
@ -384,36 +443,26 @@ bool contentFileSystemModel::createNewProgram(QMdiArea * mdiwin, QString filenam
QDir dir(path);
QFileInfo fileinfo(dir,filename+"."+getFileType(HP_PROG));
/*
QFile file(fileinfo.absoluteFilePath());
if (file.open(QIODevice::WriteOnly)) {
QTextStream out(&file);
out << "" << endl;
} else {
qWarning()<<tr("Could not open file");
}
file.close();
*/
qDebug()<<"Create new program pressed";
return openFile(mdiwin,fileinfo);
}
//create a new note file
bool contentFileSystemModel::createNewNote(QMdiArea * mdiwin,QString foldername) {
bool contentFileSystemModel::createNewNote(QMdiArea * mdiwin,QString filename) {
QSettings appSettings("IRGP","QtHPconnect");
QString path=appSettings.value("contentPath").toString();
QDir dir(path);
QFileInfo fileinfo(dir,filename+"."+getFileType(HP_NOTE));
qDebug()<<"Create new note pressed";
return false;
qDebug()<<"Create new program pressed";
return openFile(mdiwin,fileinfo);
}
//return the file type
hp_DataStruct contentFileSystemModel::getFileType(QFileInfo info) const {
hp_DataStruct filedata;
int i;
QString suffix;
hp_DataType type=HP_MAIN;
@ -431,6 +480,21 @@ hp_DataStruct contentFileSystemModel::getFileType(QFileInfo info) const {
return filedata;
}
//return the file type
bool contentFileSystemModel::isFileType(QFileInfo info) const {
hp_DataStruct filedata;
int i;
QString suffix;
suffix=info.completeSuffix();
for (i=0;i<FILE_TYPE;i++) {
if (suffix==file_type[i])
return true;
}
return false;
}
//return the file type suffix
QString contentFileSystemModel::getFileType(hp_DataType type) const {
int i;

View file

@ -21,10 +21,11 @@
hp_MdiLogWindow::hp_MdiLogWindow(QWidget * parent)
:hp_MdiWindow(parent)
{
setMinimumSize(500,400);
setMinimumSize(200,200);
setMaximumSize(1000,1000);
setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
setup();
resize(500,400);
}
void hp_MdiLogWindow::setup() {

View file

@ -21,6 +21,9 @@
#include <hptreeitem.h>
#include <hpdata.h>
#include <abstractdata.h>
#include <QBoxLayout>
#include <QToolBar>
#include <QToolButton>
//Called by the calculator Window
hp_mdiTextEdit::hp_mdiTextEdit(QWidget * parent,hpTreeItem * treeItem, AbstractData * calcData)
@ -28,7 +31,7 @@ hp_mdiTextEdit::hp_mdiTextEdit(QWidget * parent,hpTreeItem * treeItem, AbstractD
{
setMinimumSize(200,200);
setMaximumSize(1000,1000);
setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
qDebug()<<"hp_mdiTextEdit::hp_mdiTextEdit 1";
hptreeitem=treeItem;
@ -51,7 +54,7 @@ hp_mdiTextEdit::hp_mdiTextEdit(QWidget * parent, QFileInfo filedata, AbstractDat
qDebug()<<"hp_mdiTextEdit::hp_mdiTextEdit 2";
setMinimumSize(200,200);
setMaximumSize(1000,1000);
setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
// setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
calculator=QStringLiteral("Content: ");
content=true;
hptreeitem=nullptr;
@ -61,6 +64,7 @@ hp_mdiTextEdit::hp_mdiTextEdit(QWidget * parent, QFileInfo filedata, AbstractDat
type=calcData->getType();
setup();
setWindowTitle(calculator+filename);
resize(450,350);
}
void hp_mdiTextEdit::setup() {
@ -92,7 +96,41 @@ void hp_mdiTextEdit::setup() {
else {
qDebug()<<"hp_mdiTextEdit::setup - Data Null";
}
setWidget(textEdit);
QIcon save(":/icons/save_22x22.png");
QAction * actionSave= new QAction(save,"Save",this);
QWidget * top = new QWidget();
QBoxLayout * layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
top->setLayout(layout);
QToolBar * toolbar = new QToolBar("Save");
toolbar->addAction(actionSave);
QToolButton *saveButton=new QToolButton();
QMenu * menu = new QMenu(saveButton);
menu->addAction(actionSave);
layout->setMenuBar(menu);
// layout->addWidget(toolbar);
// QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
// sizePolicy1.setHorizontalStretch(0);
// sizePolicy1.setVerticalStretch(0);
// sizePolicy1.setHeightForWidth(textEdit->sizePolicy().hasHeightForWidth());
// textEdit->setSizePolicy(sizePolicy1);
// layout->setSizeConstraint(QLayout::SetNoConstraint);
layout->addWidget(textEdit);
setWidget(top);
connect(actionSave,SIGNAL(triggered()),this,SLOT(eventSave()));
return;
}
void hp_mdiTextEdit::eventSave(){
save();
}
bool hp_mdiTextEdit::save(){
@ -147,7 +185,6 @@ bool hp_mdiTextEdit::maybeSave()
return true;
}
void hp_mdiTextEdit::show() {
if (textEdit)

View file

@ -17,6 +17,10 @@
#include "hp_mdiwindow.h"
#include <QDebug>
#include <QMenu>
#include <QToolBar>
#include <QToolButton>
#include <QBoxLayout>
hp_MdiWindow::hp_MdiWindow(QWidget * parent)
:QMdiSubWindow(parent)
@ -32,6 +36,10 @@ void hp_MdiWindow::setup() {
}
void hp_MdiWindow::eventSave() {
qDebug()<<"Save pressed";
}
bool hp_MdiWindow::save() {
qDebug()<<"hp_MdiWindow::save";

View file

@ -29,7 +29,30 @@
</item>
</layout>
</widget>
<widget class="QToolBar" name="toolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionSave"/>
</widget>
<action name="actionSave">
<property name="icon">
<iconset resource="qthpconnect.qrc">
<normaloff>:/icons/save_22x22.png</normaloff>:/icons/save_22x22.png</iconset>
</property>
<property name="text">
<string>Save</string>
</property>
</action>
</widget>
<resources/>
<resources>
<include location="qthpconnect.qrc"/>
</resources>
<connections/>
</ui>

View file

@ -44,6 +44,7 @@ public:
void deleteFile(QModelIndex &index);
void renameFile(QModelIndex &index, QString newName);
hp_DataStruct getFileType(QFileInfo info) const;
bool isFileType(QFileInfo info) const;
QString getFileType(hp_DataType) const;
AbstractData * readFile(QFileInfo fileinfo) const;
int writeFile(QFileInfo fileinfo, QByteArray data_in) const;

View file

@ -62,6 +62,7 @@ public:
signals:
public slots:
void eventSave();
};

View file

@ -39,6 +39,9 @@ public:
void show();
~hp_MdiWindow();
private slots:
void eventSave();
};
#endif // HP_MDIWINDOW_H

View file

@ -27,7 +27,7 @@ textEditor::textEditor(QWidget *parent) :
QSettings appSettings("IRGP","QtHPconnect");
wParent = parent;
setAttribute(Qt::WA_DeleteOnClose);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
// setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
isUntitled = true;
defaultPath=QDir(appSettings.value("contentPath").toString());