HAL
special_log_content_manager.cpp
Go to the documentation of this file.
5 
6 #include <QTimer>
7 #include <QDateTime>
8 #include <QDir>
9 #include <QFileInfo>
10 #include <QPixmap>
11 #include <QDebug>
12 #include <QGuiApplication>
13 #include <QScreen>
14 #include <QTabWidget>
15 #include <QPlainTextEdit> //the python code editor
16 #include <QWindow>
17 #include <QWindowList>
18 #include <QPainter>
19 
20 namespace hal
21 {
23  mTimer(new QTimer(this)), mPythonEditor(python_editor)
24  {
25  mScreenshotSubPath = "/screenshots";
26  mPythonContentSubPath = "/pythoneditors";
27 
30  }
31 
33  {
34 
35  }
36 
38  {
39  QString halFileName = FileManager::get_instance()->fileName();
40 
41  if(halFileName.isEmpty() || qGuiApp->topLevelWindows().isEmpty())
42  return;
43 
44  QDir halFileDir = QFileInfo(halFileName).absoluteDir();
45  QString halFileNameSubPath = "/" + QFileInfo(halFileName).baseName();
46  QString screenshotPath = halFileDir.path() + halFileNameSubPath + mScreenshotSubPath;
47  if(!halFileDir.exists(screenshotPath))
48  {
49  if(!halFileDir.mkpath(screenshotPath))
50  {
51  qDebug() << "Failed to create screenshots directory.";
52  return;
53  }
54  }
55 
56  QString fileName = "Screenshot_" +QString::number(QDateTime::currentDateTime().toTime_t());
57  QString fileType = "png";
58 
59  QList<QPixmap> pixmapList;
60  int totalWidth = 0;
61  int maxHeight = 0;
62  for(int i = 0; i < qGuiApp->topLevelWindows().size(); i++)
63  {
64  QWidget* found_wid = QWidget::find(qGuiApp->topLevelWindows().at(i)->winId());
65  if(found_wid)
66  {
67  QPixmap p(found_wid->size());
68  found_wid->render(&p);
69  totalWidth += p.width();
70  maxHeight = (maxHeight > p.height()) ? maxHeight : p.height();
71  pixmapList.append(p);
72  }
73  }
74 
75  QImage image(totalWidth, maxHeight, QImage::Format_RGB32);
76  QPainter painter(&image);
77  int curr_x = 0;
78  for(int i = 0; i < pixmapList.size(); i++)
79  {
80  QPixmap currPixmap = pixmapList.at(i);
81  painter.drawPixmap(curr_x, 0, currPixmap);
82  painter.fillRect(curr_x, currPixmap.height(), currPixmap.width(), maxHeight-currPixmap.height(),Qt::white);
83  curr_x += currPixmap.width();
84  }
85 
86  if(!image.save(screenshotPath + "/" + fileName + "." + fileType))
87  qDebug() << "Could not save image!";
88  }
89 
91  {
92  QString halFileName = FileManager::get_instance()->fileName();
93 
94  if(!mPythonEditor || !mPythonEditor->getTabWidget() || halFileName.isEmpty())
95  return;
96 
97  QDir halFileDir = QFileInfo(halFileName).absoluteDir();
98  QString halFileNameSubPath = "/" + QFileInfo(halFileName).baseName();
99  QString pythonEditorDumpPath = halFileDir.path() + halFileNameSubPath + mPythonContentSubPath;
100  if(!halFileDir.exists(pythonEditorDumpPath))
101  {
102  if(!halFileDir.mkpath(pythonEditorDumpPath))
103  {
104  qDebug() << "Failed to create python editor dumb directory.";
105  return;
106  }
107  }
108 
109  QTabWidget* pythonTabWidget = mPythonEditor->getTabWidget();
110  QString fileName = "Pythoncodeeditors_" + QString::number(QDateTime::currentDateTime().toTime_t());
111  QString fileType = "txt";
112  QFile file(pythonEditorDumpPath + "/" + fileName + "." + fileType);
113 
115  {
116  qDebug() << "Could not open file with path: " << file.fileName() << ". Abort.";
117  return;
118  }
119 
120  QTextStream textStream(&file);
121  textStream << "Number of tabs: " << QString::number(pythonTabWidget->count()) << "\n";
122 
123  for(int i = 0; i < pythonTabWidget->count(); i++)
124  {
125  QPlainTextEdit* python_editor = dynamic_cast<QPlainTextEdit*>(pythonTabWidget->widget(i));
126  QString content = "";
127 
128  if(python_editor)
129  content = python_editor->toPlainText();
130  else
131  content = "Could not get python code editor content.";
132 
133  textStream << "---------------------Start of new tab------------------------\nTabnumber: " << i
134  << "\nName: "<< pythonTabWidget->tabText(i) << "\nContent:\n" << content << "\n";
135 
136  }
137  file.close();
138  }
139 
141  {
142  mTimer->start(msec);
143  }
144 
145 }
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
static FileManager * get_instance()
QString fileName() const
Main widget that combines all neccessary functionality to develop in python (for hal).
Definition: python_editor.h:99
QTabWidget * getTabWidget()
SpecialLogContentManager(QObject *parent=nullptr, PythonEditor *python_editor=nullptr)
QDateTime currentDateTime()
bool exists() const const
bool mkpath(const QString &dirPath) const const
QString path() const const
QDir absoluteDir() const const
QString baseName() const const
bool save(const QString &fileName, const char *format, int quality) const const
void append(const T &value)
const T & at(int i) const const
int size() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void fillRect(const QRectF &rectangle, const QBrush &brush)
int height() const const
int width() const const
QString toPlainText() const const
bool isEmpty() const const
QString number(int n, int base)
QString tabText(int index) const const
QWidget * widget(int index) const const
void start(int msec)
void timeout()
QWidget * find(WId id)
void render(QPaintDevice *target, const QPoint &targetOffset, const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)