Commit 8ed8554c authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

CodePaster: Allow for editing snippets.

Introduce separate plain text mode parallel to diff-chunk mode
using a stacked widget with a QPlainTextEdit.

Task-number: QTCREATORBUG-3859
parent 7e626a9f
......@@ -217,16 +217,18 @@ static inline void fixSpecialCharacters(QString &data)
void CodepasterPlugin::post(QString data, const QString &mimeType)
{
fixSpecialCharacters(data);
FileDataList lst = splitDiffToFiles(data.toLatin1());
QString username = m_settings->username;
QString description;
QString comment;
QString protocolName;
const QString username = m_settings->username;
PasteView view(m_protocols, mimeType, 0);
view.setProtocol(m_settings->protocol);
view.show(username, description, comment, lst);
const FileDataList diffChunks = splitDiffToFiles(data.toLatin1());
if (diffChunks.isEmpty()) {
view.show(username, QString(), QString(), data);
} else {
view.show(username, QString(), QString(), diffChunks);
}
}
void CodepasterPlugin::fetch()
......
......@@ -55,7 +55,8 @@ PasteView::PasteView(const QList<Protocol *> protocols,
QDialog(parent),
m_protocols(protocols),
m_commentPlaceHolder(tr("<Comment>")),
m_mimeType(mt)
m_mimeType(mt),
m_mode(DiffChunkMode)
{
m_ui.setupUi(this);
......@@ -95,6 +96,9 @@ QString PasteView::comment() const
QByteArray PasteView::content() const
{
if (m_mode == PlainTextMode)
return m_ui.plainTextEdit->toPlainText().toUtf8();
QByteArray newContent;
for (int i = 0; i < m_ui.uiPatchList->count(); ++i) {
QListWidgetItem *item = m_ui.uiPatchList->item(i);
......@@ -121,28 +125,15 @@ void PasteView::protocolChanged(int p)
m_ui.uiComment->setEnabled(caps & Protocol::PostCommentCapability);
}
int PasteView::show(const QString &user, const QString &description, const QString &comment,
const FileDataList &parts)
void PasteView::setupDialog(const QString &user, const QString &description, const QString &comment)
{
m_ui.uiUsername->setText(user);
m_ui.uiDescription->setText(description);
m_ui.uiComment->setPlainText(comment.isEmpty() ? m_commentPlaceHolder : comment);
}
if (comment.isEmpty())
m_ui.uiComment->setPlainText(m_commentPlaceHolder);
else
m_ui.uiComment->setPlainText(comment);
QByteArray content;
m_parts = parts;
m_ui.uiPatchList->clear();
foreach (const FileData &part, parts) {
QListWidgetItem *itm = new QListWidgetItem(part.filename, m_ui.uiPatchList);
itm->setCheckState(Qt::Checked);
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
content += part.content;
}
m_ui.uiPatchView->setPlainText(content);
int PasteView::showDialog()
{
m_ui.uiDescription->setFocus();
m_ui.uiDescription->selectAll();
......@@ -159,6 +150,37 @@ int PasteView::show(const QString &user, const QString &description, const QStri
return ret;
}
// Show up with checkable list of diff chunks.
int PasteView::show(const QString &user, const QString &description,
const QString &comment, const FileDataList &parts)
{
setupDialog(user, description, comment);
m_ui.uiPatchList->clear();
m_parts = parts;
m_mode = DiffChunkMode;
QByteArray content;
foreach (const FileData &part, parts) {
QListWidgetItem *itm = new QListWidgetItem(part.filename, m_ui.uiPatchList);
itm->setCheckState(Qt::Checked);
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
content += part.content;
}
m_ui.stackedWidget->setCurrentIndex(0);
m_ui.uiPatchView->setPlainText(content);
return showDialog();
}
// Show up with editable plain text.
int PasteView::show(const QString &user, const QString &description,
const QString &comment, const QString &content)
{
setupDialog(user, description, comment);
m_mode = PlainTextMode;
m_ui.stackedWidget->setCurrentIndex(1);
m_ui.plainTextEdit->setPlainText(content);
return showDialog();
}
void PasteView::accept()
{
const int index = m_ui.protocolBox->currentIndex();
......@@ -170,8 +192,12 @@ void PasteView::accept()
if (!Protocol::ensureConfiguration(protocol, this))
return;
const QByteArray data = content();
if (data.isEmpty())
return;
const Protocol::ContentType ct = Protocol::contentType(m_mimeType);
protocol->paste(content(), ct, user(), comment(), description());
protocol->paste(data, ct, user(), comment(), description());
// Store settings and close
QSettings *settings = Core::ICore::instance()->settings();
settings->beginGroup(QLatin1String(groupC));
......
......@@ -45,13 +45,25 @@ class PasteView : public QDialog
{
Q_OBJECT
public:
enum Mode
{
// Present a list of read-only diff chunks which the user can check for inclusion
DiffChunkMode,
// Present plain, editable text.
PlainTextMode
};
explicit PasteView(const QList<Protocol *> protocols,
const QString &mimeType,
QWidget *parent);
~PasteView();
// Show up with checkable list of diff chunks.
int show(const QString &user, const QString &description, const QString &comment,
const FileDataList &parts);
// Show up with editable plain text.
int show(const QString &user, const QString &description, const QString &comment,
const QString &content);
void setProtocol(const QString &protocol);
......@@ -68,12 +80,16 @@ private slots:
void protocolChanged(int);
private:
void setupDialog(const QString &user, const QString &description, const QString &comment);
int showDialog();
const QList<Protocol *> m_protocols;
const QString m_commentPlaceHolder;
const QString m_mimeType;
Ui::ViewDialog m_ui;
FileDataList m_parts;
Mode m_mode;
};
} // namespace CodePaster
#endif // VIEW_H
......@@ -6,18 +6,18 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>500</height>
<width>670</width>
<height>678</height>
</rect>
</property>
<property name="windowTitle">
<string>Send to Codepaster</string>
</property>
<layout class="QVBoxLayout" name="vboxLayout1">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<widget class="QLabel" name="protocolLabel">
<property name="text">
<string>Protocol:</string>
</property>
......@@ -27,7 +27,7 @@
<widget class="QComboBox" name="protocolBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<widget class="QLabel" name="userLabel">
<property name="text">
<string>&amp;Username:</string>
</property>
......@@ -44,7 +44,7 @@
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>&amp;Description:</string>
</property>
......@@ -89,68 +89,83 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Parts to Send to Server</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
<layout class="QVBoxLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QListWidget" name="uiPatchList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>Patch 1</string>
<widget class="QStackedWidget" name="stackedWidget">
<widget class="QWidget" name="diffChunkPage">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Parts to Send to Server</string>
</property>
</item>
<item>
<property name="text">
<string>Patch 2</string>
<property name="flat">
<bool>true</bool>
</property>
</item>
</widget>
</item>
<item>
<widget class="CodePaster::ColumnIndicatorTextEdit" name="uiPatchView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>3</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
<layout class="QVBoxLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QListWidget" name="uiPatchList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>Patch 1</string>
</property>
</item>
<item>
<property name="text">
<string>Patch 2</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="CodePaster::ColumnIndicatorTextEdit" name="uiPatchView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>3</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="plainTextPage">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
......
......@@ -56,8 +56,7 @@ FileDataList splitDiffToFiles(const QByteArray &data)
"\\n\\-\\-\\- ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*";
} else {
ret.append(FileData("<not a diff>", data));
return ret;
return FileDataList();
}
int splitIndex = 0, previousSplit = -1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment