summaryrefslogtreecommitdiffstats
path: root/kommander/editor/newformimpl.cpp
blob: 810bdf62a8a98d053e04ce696ba067cad52523d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**********************************************************************
 This file is based on TQt Designer, Copyright (C) 2000 Trolltech AS. �All rights reserved.

 This file may be distributed and/or modified under the terms of the
 GNU General Public License version 2 as published by the Free Software
 Foundation and appearing in the file LICENSE.GPL included in the
 packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

 See http://www.trolltech.com/gpl/ for GPL licensing information.

 Modified for Kommander:
  (C) 2004      Michal Rudolf <[email protected]>

**********************************************************************/

#include "newformimpl.h"
#include "mainwindow.h"
#include "pixmapchooser.h"
#include "metadatabase.h"
#include "formwindow.h"
#include "widgetfactory.h"
#include "widgetdatabase.h"
#include "actioneditorimpl.h"
#include "hierarchyview.h"
#include "resource.h"
#include "formfile.h"

#include <tqiconview.h>
#include <tqlabel.h>
#include <tqfileinfo.h>
#include <tqdir.h>
#include <tqregexp.h>
#include <tqpushbutton.h>
#include <stdlib.h>
#include <tqcombobox.h>
#include <tqworkspace.h>
#include <tqmessagebox.h>

#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>

#include "dialog.h"

static int _forms = 0;

FormItem::FormItem(TQIconView *view, const TQString &text)    : NewItem(view, text)
{
}

void FormItem::insert()
{
  TQString n = "Form" + TQString::number(++_forms);
  FormWindow *fw = 0;
  FormFile *ff = new FormFile(FormFile::createUnnamedFileName(), true);
  fw = new FormWindow(ff, MainWindow::self, MainWindow::self->qWorkspace(), n);
  MetaDataBase::addEntry(TQT_TQOBJECT(fw));
  TQWidget *w = 0L;
  if (fType == Dialog) 
  {
    w = WidgetFactory::create(WidgetDatabase::idFromClassName("Dialog"), fw, n.latin1());
    fw->setMainContainer(w);
  } 
  else if (fType == Wizard)
  {
    w = WidgetFactory::create(WidgetDatabase::idFromClassName("Wizard"), fw, n.latin1());
    fw->setMainContainer(w);
  }
  if (w)
  {
    w->setProperty("useInternalParser", true);
    MetaDataBase::setPropertyChanged(TQT_TQOBJECT(w), "useInternalParser", true);
  }
  fw->setCaption(n);
  fw->resize(600, 480);
  MainWindow::self->insertFormWindow(fw);

  // the wizard might have changed a lot, lets update everything
  MainWindow::self->actioneditor()->setFormWindow(fw);
  MainWindow::self->objectHierarchy()->setFormWindow(fw, fw);
  fw->killAccels(TQT_TQOBJECT(fw));
  fw->setFocus();
}



CustomFormItem::CustomFormItem(TQIconView *view, const TQString &text)
    : NewItem(view, text)
{
}

static void unifyFormName(FormWindow *fw, TQWorkspace *qworkspace)
{
  TQStringList lst;
  TQWidgetList windows = qworkspace->windowList();
  for (TQWidget* w = windows.first(); w; w = windows.next())
    if (w != fw)
      lst << w->name();
  
  if (lst.findIndex(fw->name()) == -1)
    return;
  TQString origName = fw->name();
  TQString n = origName;
  int i = 1;
  while (lst.findIndex(n) != -1)
    n = origName + TQString::number(i++);
  fw->setName(n);
  fw->setCaption(n);
}

void CustomFormItem::insert()
{
  TQString filename = templateFileName();
  if (!filename.isEmpty() && TQFile::exists(filename))
  {
    Resource resource(MainWindow::self);
    FormFile *ff = new FormFile(filename, true);
    if (!resource.load(ff))
    {
      TQMessageBox::information(MainWindow::self, i18n("Load Template"),
          i18n("Could not load form description from template '%1'").arg(filename));
      delete ff;
      return;
    }
    ff->setFileName(TQString());
    if (MainWindow::self->formWindow())
    {
      MainWindow::self->formWindow()->setFileName(TQString());
      unifyFormName(MainWindow::self->formWindow(), MainWindow::self->qWorkspace());
    }
  }
}


NewForm::NewForm(TQWidget *parent, const TQString &templatePath)
    : NewFormBase(parent, 0, true)
{
  connect(helpButton, TQT_SIGNAL(clicked()), MainWindow::self, TQT_SLOT(showDialogHelp()));

  TQIconViewItem *cur = 0;
  FormItem *fi = new FormItem(templateView, i18n("Dialog"));
  allItems.append(fi);
  fi->setFormType(FormItem::Dialog);
  fi->setPixmap(PixmapChooser::loadPixmap("newform.xpm"));
  fi->setDragEnabled(false);
  cur = fi;
  fi = new FormItem(templateView, i18n("Wizard"));
  allItems.append(fi);
  fi->setFormType(FormItem::Wizard);
  fi->setPixmap(PixmapChooser::loadPixmap("newform.xpm"));
  fi->setDragEnabled(false);


  TQStringList searchPaths = TDEGlobal::dirs()->findDirs("data", "kmdr-editor/templates");
  if (!templatePath.isEmpty())
    searchPaths.append(templatePath);
  
  
  // search each path
  for (TQStringList::ConstIterator it = searchPaths.begin(); it != searchPaths.end(); ++it)
  {
    if (!TQFile::exists(*it))
      continue;
    TQDir dir(*it);
    const TQFileInfoList* fileList = dir.entryInfoList(TQDir::DefaultFilter, TQDir::DirsFirst | TQDir::Name);
    if (fileList)
      for (TQFileInfoListIterator fit(*fileList); fit.current(); ++fit)
      {
        TQFileInfo* fi = fit.current();
        if (!fi->isFile() || fi->extension() != "kmdr")
          continue;
        TQString name = fi->baseName();
        name = name.replace("_", " ");
        CustomFormItem *ci = new CustomFormItem(templateView, name);
        allItems.append(ci);
        ci->setDragEnabled(false);
        ci->setPixmap(PixmapChooser::loadPixmap("newform.xpm"));
        ci->setTemplateFile(fi->absFilePath());
      }
  }
  templateView->viewport()->setFocus();
  templateView->setCurrentItem(cur);
}

void NewForm::accept()
{
  if (!templateView->currentItem())
    return;
  ((NewItem *) templateView->currentItem())->insert();
  NewFormBase::accept();
}


void NewForm::itemChanged(TQIconViewItem *item)
{
    Q_UNUSED(item);
}

#include "newformimpl.moc"