Applied a bit of foutf's suggestions. Replace uploadQueue.size() with .empty() and swap if/else bodies, move non empty queue check outside of the timer on line 60, move getImageFileFormat to an anonymous namespace, rename pasteFromClipoard to upload(), removed usesless comment, shortened message on line 83, use QMimeData.hasUrls() and QMimeData.urls(), moved GIF format case in upload() more to the top, call original functions in canInsertFromMimeData and dragEnterEvent which are overriden

This commit is contained in:
Mm2PL 2019-09-25 22:21:26 +02:00
parent 298c013fa0
commit 4e9951371f
No known key found for this signature in database
GPG key ID: 1C400DA5602DE62E
4 changed files with 99 additions and 97 deletions

View file

@ -37,7 +37,12 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
.multiPart(payload) .multiPart(payload)
.onSuccess([&textEdit, channel](NetworkResult result) -> Outcome { .onSuccess([&textEdit, channel](NetworkResult result) -> Outcome {
textEdit.insertPlainText(result.getData() + QString(" ")); textEdit.insertPlainText(result.getData() + QString(" "));
if (uploadQueue.size()) if (uploadQueue.empty())
{
channel->addMessage(makeSystemMessage(
QString("Your image has been uploaded.")));
}
else
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
QString("Your image has been uploaded. %1 left. Please " QString("Your image has been uploaded. %1 left. Please "
@ -49,19 +54,14 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
// 2 seconds for the timer that's there not to spam Nuuls' server // 2 seconds for the timer that's there not to spam Nuuls' server
// and 1 second of actual uploading. // and 1 second of actual uploading.
} }
else
{
channel->addMessage(makeSystemMessage(
QString("Your image has been uploaded.")));
}
isUploading = false; isUploading = false;
QTimer::singleShot(2000, [channel, &textEdit]() {
if (uploadQueue.size()) if (uploadQueue.size())
{ {
QTimer::singleShot(2000, [channel, &textEdit]() {
uploadImageToNuuls(uploadQueue.front(), channel, textEdit); uploadImageToNuuls(uploadQueue.front(), channel, textEdit);
uploadQueue.pop(); uploadQueue.pop();
}
}); });
}
return Success; return Success;
}) })
.onError([channel](NetworkResult result) -> bool { .onError([channel](NetworkResult result) -> bool {
@ -73,31 +73,14 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
}) })
.execute(); .execute();
} }
QString getImageFileFormat(QString path)
{
static QStringList LIST_OF_IMAGE_FORMATS = {".png", ".jpg", ".jpeg"};
for (QString i : LIST_OF_IMAGE_FORMATS)
{
if (path.endsWith(i))
{
return i.replace('.', "");
}
}
return QString();
}
void pasteFromClipboard(const QMimeData *source, ChannelPtr channel, void upload(const QMimeData *source, ChannelPtr channel,
ResizingTextEdit &outputTextEdit) ResizingTextEdit &outputTextEdit)
{ {
/*
http://localhost:7494/upload?password=xd
default port and password for nuuls' filehost.
*/
if (isUploading) if (isUploading)
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
QString("You are already uploading an image. " QString("Please wait until the upload finishes.")));
"Please wait until the upload finishes.")));
return; return;
} }
@ -109,29 +92,29 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
uploadImageToNuuls({source->data("image/png"), "png"}, channel, uploadImageToNuuls({source->data("image/png"), "png"}, channel,
outputTextEdit); outputTextEdit);
} }
else if (source->hasFormat("text/uri-list")) else if (source->hasFormat("image/jpeg"))
{ {
QStringList potientialPathsToSend = uploadImageToNuuls({source->data("image/jpeg"), "jpeg"}, channel,
QString(source->data("text/uri-list").toStdString().c_str()) outputTextEdit);
.split("\r\n");
for (QString path : potientialPathsToSend)
{
if (path.isEmpty())
{
break;
} }
else else if (source->hasFormat("image/gif"))
{ {
if (getImageFileFormat(path) != QString()) uploadImageToNuuls({source->data("image/gif"), "gif"}, channel,
outputTextEdit);
}
else if (source->hasUrls())
{
for (QUrl path : source->urls())
{
if (getImageFileFormat(path.toLocalFile()) != QString())
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
QString("Uploading image: %1").arg(path))); QString("Uploading image: %1").arg(path.toLocalFile())));
QImage img = QImage(QUrl(path).toLocalFile()); QImage img = QImage(path.toLocalFile());
if (img.isNull()) if (img.isNull())
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(
QString("Couldn't load image :("))); makeSystemMessage(QString("Couldn't load image :(")));
return; return;
} }
QByteArray imageData; QByteArray imageData;
@ -142,16 +125,16 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
TypedBytes data = {imageData, "png"}; TypedBytes data = {imageData, "png"};
uploadQueue.push(data); uploadQueue.push(data);
} }
else if (path.endsWith(".gif")) else if (path.toLocalFile().endsWith(".gif"))
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
QString("Uploading GIF: %1").arg(path))); QString("Uploading GIF: %1").arg(path.toLocalFile())));
QFile file(QUrl(path).toLocalFile()); QFile file(path.toLocalFile());
bool isOkay = file.open(QIODevice::ReadOnly); bool isOkay = file.open(QIODevice::ReadOnly);
if (!isOkay) if (!isOkay)
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(
QString("Failed to open file. :("))); makeSystemMessage(QString("Failed to open file. :(")));
return; return;
} }
TypedBytes data = {file.readAll(), "gif"}; TypedBytes data = {file.readAll(), "gif"};
@ -163,8 +146,7 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
QString("Cannot upload file: %1, not an image") QString("Cannot upload file: %1, not an image")
.arg(path))); .arg(path.toLocalFile())));
}
} }
} }
if (uploadQueue.size()) if (uploadQueue.size())
@ -173,11 +155,6 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
uploadQueue.pop(); uploadQueue.pop();
} }
} }
else if (source->hasFormat("image/gif"))
{
TypedBytes data = {source->data("image/gif"), "gif"};
uploadImageToNuuls(data, channel, outputTextEdit);
}
else else
{ // not PNG, try loading it into QImage and save it to a PNG. { // not PNG, try loading it into QImage and save it to a PNG.
QImage image = qvariant_cast<QImage>(source->imageData()); QImage image = qvariant_cast<QImage>(source->imageData());
@ -190,3 +167,19 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
} }
} }
} // namespace chatterino } // namespace chatterino
namespace {
QString getImageFileFormat(QString path)
{
static QStringList listOfImageFormats = {".png", ".jpg", ".jpeg"};
for (const QString &format : listOfImageFormats)
{
if (path.endsWith(format))
{
return format.mid(1);
}
}
return QString();
}
} // namespace

View file

@ -9,11 +9,14 @@ struct TypedBytes {
QByteArray data; QByteArray data;
QString type; QString type;
}; };
void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel, void upload(QByteArray imageData, ChannelPtr channel,
ResizingTextEdit &textEdit, std::string format); ResizingTextEdit &textEdit, std::string format);
void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel, void upload(TypedBytes imageData, ChannelPtr channel,
ResizingTextEdit &textEdit); ResizingTextEdit &textEdit);
QString getImageFileFormat(QString path); void upload(const QMimeData *source, ChannelPtr channel,
void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
ResizingTextEdit &outputTextEdit); ResizingTextEdit &outputTextEdit);
} // namespace chatterino } // namespace chatterino
namespace {
QString getImageFileFormat(QString path);
}

View file

@ -264,7 +264,10 @@ bool ResizingTextEdit::canInsertFromMimeData(const QMimeData *source) const
{ {
return true; return true;
} }
return false; else
{
return QTextEdit::canInsertFromMimeData(source);
}
} }
void ResizingTextEdit::insertFromMimeData(const QMimeData *source) void ResizingTextEdit::insertFromMimeData(const QMimeData *source)
@ -285,8 +288,12 @@ void ResizingTextEdit::dragEnterEvent(QDragEnterEvent *event)
{ {
event->acceptProposedAction(); event->acceptProposedAction();
} }
// QTextEdit doesn't implement dragEnterEvent, so there's nothing to call here. else
{
QAbstractScrollArea::dragEnterEvent(event);
} }
}
void ResizingTextEdit::dropEvent(QDropEvent *event) void ResizingTextEdit::dropEvent(QDropEvent *event)
{ {
if (event->mimeData()->hasImage() || event->mimeData()->hasUrls()) if (event->mimeData()->hasImage() || event->mimeData()->hasUrls())

View file

@ -209,8 +209,7 @@ Split::Split(QWidget *parent)
[this] { this->focusLost.invoke(); }); [this] { this->focusLost.invoke(); });
this->input_->ui_.textEdit->pastedImage.connect( this->input_->ui_.textEdit->pastedImage.connect(
[this](const QMimeData *source) { [this](const QMimeData *source) {
pasteFromClipboard(source, this->getChannel(), upload(source, this->getChannel(), *this->input_->ui_.textEdit);
*this->input_->ui_.textEdit);
}); });
} }