58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#include "clipboardpopup.h"
|
|
#include "ui_clipboardpopup.h"
|
|
|
|
#include <QClipboard>
|
|
|
|
ClipboardPopup::ClipboardPopup(Direction dir, QWidget* parent) : QDialog(parent), ui(new Ui::ClipboardPopup), direction(dir) {
|
|
ui->setupUi(this);
|
|
|
|
if(direction == Direction::Paste) {
|
|
this->ui->copyButton->hide();
|
|
this->ui->text->setPlaceholderText(ClipboardPopup::tr("Paste here"));
|
|
this->ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
|
|
|
|
QObject::connect(this->ui->text, &QTextEdit::textChanged, this, &ClipboardPopup::textPasted);
|
|
} else {
|
|
// copy
|
|
this->ui->text->setReadOnly(true);
|
|
}
|
|
|
|
QObject::connect(this->ui->copyButton, &QPushButton::pressed, this, &ClipboardPopup::copyButton);
|
|
}
|
|
|
|
ClipboardPopup::~ClipboardPopup() {
|
|
delete this->ui;
|
|
}
|
|
|
|
void ClipboardPopup::textPasted() {
|
|
int newTextSize = this->getText().size();
|
|
if(newTextSize - 1 > lastKnownTextSize && newTextSize > 1) {
|
|
// asume that something was pasted
|
|
accept();
|
|
return;
|
|
}
|
|
|
|
if(newTextSize > 0) {
|
|
this->ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
|
|
}
|
|
|
|
lastKnownTextSize = newTextSize;
|
|
}
|
|
|
|
void ClipboardPopup::setText(QString text) {
|
|
this->ui->text->setText(text);
|
|
this->ui->text->selectAll();
|
|
}
|
|
|
|
QString ClipboardPopup::getText() const {
|
|
return this->ui->text->toPlainText();
|
|
}
|
|
|
|
void ClipboardPopup::copyButton() {
|
|
QClipboard* clip = qApp->clipboard();
|
|
clip->setText(this->ui->text->toPlainText());
|
|
|
|
// close this dialog
|
|
accept();
|
|
}
|