File indexing completed on 2024-05-19 05:42:28

0001 // ct_lvtqtw_textview.cpp                                             -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtqtw_textview.h>
0021 
0022 #include <QIODevice>
0023 #include <QTabWidget>
0024 #include <QTemporaryFile>
0025 #include <QUuid>
0026 
0027 using namespace Codethink::lvtqtw;
0028 
0029 struct TextView::Private {
0030     int id;
0031     QTemporaryFile tmp;
0032 
0033     explicit Private(int inId): id(inId)
0034     {
0035     }
0036 };
0037 
0038 TextView::TextView(int id, QWidget *parent): QTextEdit(parent), d(std::make_unique<TextView::Private>(id))
0039 {
0040 }
0041 
0042 TextView::~TextView() = default;
0043 
0044 void TextView::appendText(const QString& text)
0045 {
0046     if (text.isEmpty()) {
0047         return;
0048     }
0049 
0050     if (!d->tmp.isOpen()) {
0051         d->tmp.open();
0052     }
0053     d->tmp.seek(d->tmp.size());
0054     d->tmp.write(text.toLocal8Bit() + "\n");
0055 
0056     if (isVisible()) {
0057         QTextEdit::append(text);
0058     }
0059 }
0060 
0061 void TextView::showEvent(QShowEvent *ev)
0062 {
0063     Q_UNUSED(ev);
0064     d->tmp.seek(0);
0065     setText(d->tmp.readAll());
0066 }
0067 
0068 void TextView::hideEvent(QHideEvent *ev)
0069 {
0070     Q_UNUSED(ev);
0071     setText(QString());
0072 }
0073 
0074 void TextView::saveFileTo(const QString& path)
0075 {
0076     d->tmp.seek(0);
0077     d->tmp.copy(path);
0078 }